Im trying to convert this php function to javascript:
我试图将这个PHP函数转换为javascript:
function sanitize_words($string,$limit=false) {
preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]{1,}/u",$string,$matches,PREG_PATTERN_ORDER);
return $matches[0];
}
Basically, it takes this string:
基本上,它需要这个字符串:
$string = "Why hello, how are you?"
$array = sanitize_words($string);
And converts it into an array:
并将其转换为数组:
$array[0] = 'Why';
$array[1] = 'hello';
$array[2] = 'how';
$array[3] = 'are';
$array[4] = 'you';
It works great on php, but i have no idea on how to implement it on javascript, since there is no preg_match_all in phpjs.org. Any ideas? Thanks.
它在php上工作得很好,但我不知道如何在javascript上实现它,因为phpjs.org中没有preg_match_all。有任何想法吗?谢谢。
3 个解决方案
#1
1
Use the String.match
method, with the g
(global) flag set on the RegEx. \w
is equivalent to [a-zA-Z0-9_]
. If you really want to mimic your current pattern, use this page as a reference to convert the character properties in a JavaScript pattern.
使用String.match方法,在RegEx上设置g(全局)标志。 \ w相当于[a-zA-Z0-9_]。如果您确实想模仿当前模式,请使用此页面作为参考,以转换JavaScript模式中的字符属性。
function sanitize_words($string) {
return $string.match(/\w+/g);
}
#2
1
The JavaScript split()
function will make an array from any string using a delimiter. In this case, a space.
JavaScript split()函数将使用分隔符从任何字符串生成数组。在这种情况下,一个空间。
var str = "Why hello, how are you?".split(" ")
alert(str[0]) // = "Why"
#3
1
You don't need a regex, split will do in javascript.
你不需要正则表达式,拆分将在javascript中完成。
<script type="text/javascript">
var myString = "zero one two three four";
var mySplitResult = myString.split(" ");
for(i = 0; i < mySplitResult.length; i++){
document.write("<br /> Element " + i + " = " + mySplitResult[i]);
}
</script>
Displays:
显示:
Element 0 = zero
Element 1 = one
Element 2 = two
Element 3 = three
Element 4 = four
As a side note, in your PHP script, if all you are wanting to do is create an array of words you should use explode()
it has a LOT less overhead:
作为旁注,在你的PHP脚本中,如果你想要做的就是创建一个单词数组,你应该使用explode()它有更少的开销:
<?php
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
// to remove non alpha-numeric chars, and still less costly
$pizza = preg_replace('/[^a-zA-Z0-9\s]/', '', $pizza);
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>
#1
1
Use the String.match
method, with the g
(global) flag set on the RegEx. \w
is equivalent to [a-zA-Z0-9_]
. If you really want to mimic your current pattern, use this page as a reference to convert the character properties in a JavaScript pattern.
使用String.match方法,在RegEx上设置g(全局)标志。 \ w相当于[a-zA-Z0-9_]。如果您确实想模仿当前模式,请使用此页面作为参考,以转换JavaScript模式中的字符属性。
function sanitize_words($string) {
return $string.match(/\w+/g);
}
#2
1
The JavaScript split()
function will make an array from any string using a delimiter. In this case, a space.
JavaScript split()函数将使用分隔符从任何字符串生成数组。在这种情况下,一个空间。
var str = "Why hello, how are you?".split(" ")
alert(str[0]) // = "Why"
#3
1
You don't need a regex, split will do in javascript.
你不需要正则表达式,拆分将在javascript中完成。
<script type="text/javascript">
var myString = "zero one two three four";
var mySplitResult = myString.split(" ");
for(i = 0; i < mySplitResult.length; i++){
document.write("<br /> Element " + i + " = " + mySplitResult[i]);
}
</script>
Displays:
显示:
Element 0 = zero
Element 1 = one
Element 2 = two
Element 3 = three
Element 4 = four
As a side note, in your PHP script, if all you are wanting to do is create an array of words you should use explode()
it has a LOT less overhead:
作为旁注,在你的PHP脚本中,如果你想要做的就是创建一个单词数组,你应该使用explode()它有更少的开销:
<?php
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
// to remove non alpha-numeric chars, and still less costly
$pizza = preg_replace('/[^a-zA-Z0-9\s]/', '', $pizza);
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
?>