[英]Split string into parts by multiple conditions
我有某种字符串可以按不同的条件拆分成数组。 我尝试了不同的方法来做到这一点。 但还是不会像预期的那样。
我的字符串可以包含任何字符。 注意:字符串不包含空格
示例字符串:- $$_hello_$$-world/foo/$$_bar_$$$$_john_$$-doe
我需要拆分为,
$$_anythinghere_$$
是一部分
-
另一部分
/
另一部分
任何其他文本作为另一部分
我想将此字符串拆分为一个数组,例如
["$$_hello_$$", "-", "world", "/", "foo", "/", "$$_bar_$$", "$$_john_$$", "-", "doe"]
我期待一个香草 javascript 的解决方案。
这可能会有所帮助。
var regex = new RegExp(/(\$\$\_(.*?)\_\$\$)|(\-)|(\/)|((?:.)\w*)/, 'ig');
var str = "$$_hello_$$-world/foo/$$_bar_$$$$_john_$$-doe";
var matches = [];
while (i = regex.exec(str)) {
matches.push(i[0]);
}
console.log(matches)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.