0

我正在使用 XRegExp 正则表达式。我想在我的角色之间留一个空格,我还需要启用特殊字符。我设法添加了允许的特殊字符,但我不能允许空格。

我的正则表达式unicodeWord = XRegExp("^(\\p{L}|[0-9][\s])+$");

它允许像

欢迎

但不是

你好欢迎

//Alphanumeric validation
function isAlphanumeric(str) { 
    var unicodeWord = XRegExp("^[\p{L}\d]+(?:\s+[\p{L}\d]+)*$"); 
    result = unicodeWord.test(str);
    return result;
}


été altérée sûr générateurs

但是这个dosnt匹配这个字母数字。

4

2 回答 2

2

您需要更改您的正则表达式,例如,

unicodeWord = XRegExp("^[\\p{L}\\d]+(?:\\s[\\p{L}\\d]+)*$");
  • [\\p{L}\\d]+匹配一个或多个字母或数字。
  • (?:\\s[\\p{L}\\d]+)*后跟零个或多个(空格后跟一个或多个字母或数字

或者

unicodeWord = XRegExp("^[\\p{L}\\d]+(?:\\s[\\p{L}\\d]+)?$");

?in(?:\\s[\\p{L}\\d]+)?会将先前的标记(?:\\s[\\p{L}\\d]+)变成可选的。

于 2014-12-05T07:02:28.397 回答
1
^(?:\\p{L}|[0-9\s])+$

试试这个。看演示。

https://regex101.com/r/iY3eK8/3

于 2014-12-05T07:03:52.933 回答