简体   繁体   中英

How Can I Use Regex To Split A String on Letters and Numbers and Carets followed by digits

So I was wondering how I would go about splitting a String on specific characters/conditions using Regular Expressions like the following:

  • On digits
  • On Letters
  • On digits following a caret

Here could be an example:

var str1 = "62y^2";

Would return as an array:

[62,y,^2]


Thanks for the help.

You can use String.match() instead of split with the following regular expression ( Regex101 ):

 var str="62y^2ad23^123"; var result = str.match(/\\^?\\d+|[az]+/gi); console.log(result); 

You may try the following approach:

 var str="62y^2ad23^123"; console.log(str.split(/(\\^\\d+|[a-zA-Z]+|\\d+)/).filter(function(n){ return n != "" })); 

Try the below regex

var str = "62y^2";
str.match(/\^(\d+|[a-zA-Z]+)|[a-zA-Z]+|\d+/g); // ["62", "y", "^2"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM