JavaScript 相当于 PHP preg_split()
JavaScript 是否有与 PHP 函数 preg_split 等效的函数?
Is there an equivalent of the PHP function preg_split for JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
javascript 中的任何字符串都可以使用
string.split
函数,例如,其中
split
将正则表达式或文字字符串作为参数。Any string in javascript can be split using the
string.split
function, e.g.where
split
takes as an argument either a regular expression or a literal string.您可以将正则表达式与 split 一起使用。
问题是字符串中的转义字符,因为 (? 打开一个非捕获组,但没有相应的 } 来关闭非捕获组,它将要查找的字符串标识为 '
You can use regular expressions with split.
The problem is the escape characters in the string as the (? opens a non capturing group but there is no corresponding } to close the non capturing group it identifies the string to look for as '
如果您想支持所有 preg_split 参数,请参阅 https:// github.com/kvz/phpjs/blob/master/_workbench/pcre/preg_split.js(尽管不确定它的测试情况如何)。
请记住,JavaScript 的正则表达式语法与 PHP 的有些不同(主要是表达能力较差)。我们希望在某个时候集成 XRegExp,因为这弥补了 PHP 正则表达式的一些缺失功能(以及使用 String.split() 等函数修复了许多浏览器可靠性问题。
If you want support for all of the preg_split arguments see https://github.com/kvz/phpjs/blob/master/_workbench/pcre/preg_split.js (though not sure how well tested it is).
Just bear in mind that JavaScript's regex syntax is somewhat different from PHP's (mostly less expressive). We would like to integrate XRegExp at some point as that makes up for some of the missing features of PHP regexes (as well as fixes the many browser reliability problems with functions like String.split()).