JavaScript等效于PhP preg_split。

时间:2021-09-30 22:08:01

Is there an equivalent of the PhP function preg_split for JavaScript?

对于JavaScript来说,PhP函数preg_split是否等价?

3 个解决方案

#1


28  

Any string in javascript can be split using the string.split function, e.g.

javascript中的任何字符串都可以使用该字符串进行分割。分割函数,如

"foo:bar".split(/:/)

where split takes as an argument either a regular expression or a literal string.

split是将正则表达式或文字字符串作为参数。

#2


0  

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 '

问题是字符串中的转义字符作为(?打开一个非捕获组,但是没有相应的}来关闭它标识要查找的字符串为'

#3


0  

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).

如果您想要支持所有的preg_split参数,请参见https://github.com/kvz/phpjs/blob/master/_workbench/pcre/preg_split.js(尽管不确定它测试得有多好)。

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()).

请记住,JavaScript的regex语法与PHP的语法有些不同(大部分不那么明显)。我们希望在某个时候集成XRegExp,因为它弥补了PHP regexes所缺少的一些特性(以及使用String.split()等函数修复许多浏览器可靠性问题)。

#1


28  

Any string in javascript can be split using the string.split function, e.g.

javascript中的任何字符串都可以使用该字符串进行分割。分割函数,如

"foo:bar".split(/:/)

where split takes as an argument either a regular expression or a literal string.

split是将正则表达式或文字字符串作为参数。

#2


0  

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 '

问题是字符串中的转义字符作为(?打开一个非捕获组,但是没有相应的}来关闭它标识要查找的字符串为'

#3


0  

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).

如果您想要支持所有的preg_split参数,请参见https://github.com/kvz/phpjs/blob/master/_workbench/pcre/preg_split.js(尽管不确定它测试得有多好)。

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()).

请记住,JavaScript的regex语法与PHP的语法有些不同(大部分不那么明显)。我们希望在某个时候集成XRegExp,因为它弥补了PHP regexes所缺少的一些特性(以及使用String.split()等函数修复许多浏览器可靠性问题)。