[英]How can I use regex to split a string on everything that is not a character
How could I use vanilla javascript and regex to split on every character that is not a string? 如何使用香草javascript和正则表达式对不是字符串的每个字符进行拆分? Example:
例:
var src = "wer%55";
and have it return the array as 并将其返回为
"wer","%","55"
Thanks! 谢谢!
You can use /(\\W+)/
for splitting and make sure to group \\W+
(1+ non-word characters) to be able to return it in the resulting array: 您可以使用
/(\\W+)/
进行拆分,并确保将\\W+
(1个非单词字符)分组,以便能够在结果数组中返回它:
var src = "wer%55"; console.log(src.split(/(\\W+)/)); // add filter(Boolean) to discard empty values from result array src = "wer%55#"; console.log(src.split(/(\\W+)/)); console.log(src.split(/(\\W+)/).filter(Boolean));
Split the string by the word boundary \\b
: 用单词
\\b
分隔字符串:
var src = "wer%55$$abc33"; console.log(src.split(/\\b/));
Or 要么
Use String#match to get sequences of word / non word characters: 使用String#match获得单词/非单词字符的序列:
var src = "wer%55$$abc33"; console.log(src.match(/\\w+|\\W+/g));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.