[英]JavaScript Regex - Splitting a string into an array by the Regex pattern
[英]javaScript - splitting string in array that matches pattern
我以以下字符串为例
hello {{salutation}} {{ name }} we are glad to tell you {{ message }}
我想要一个像这样的数组:
["hello", "{{salutation}}", "{{ name }}", "we are glad to tell you", "{{ message }}"]
使用本机函数可以做到这一点吗? 还是我必须采取解决方法?
您可以将.match()
与RegExp
/[{]+[^}]+[}]+|[\\w\\s]+/g
var str = "hello {{salutation}} {{ name }} we are glad to tell you {{ message }}"; var res = str.match(/[{]+[^}]+[}]+|[\\w\\s]+/g); console.log(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.