Categories
JavaScript Answers

How to split JavaScript string on upper case characters?

Spread the love

Sometimes, we want to split JavaScript string on upper case characters.

In this article, we’ll look at how to split JavaScript string on upper case characters.

How to split JavaScript string on upper case characters?

To split JavaScript string on upper case characters, we can call the string split method with a regex.

For instance, we write

const a = "ThisIsTheStringToSplit".split(/(?=[A-Z])/);

to call split with /(?=[A-Z])/ to split the string on upper case characters.

We use ?= to keep the whole word intact after splitting.

split returns an array with the split strings.

Conclusion

To split JavaScript string on upper case characters, we can call the string split method with a regex.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *