更新时间:2023-02-25 22:37:42
使用 XRegExp
解析当前正则表达式的库:
Use XRegExp
library to parse your current regular expression:
var pattern = new XRegExp("^[0-9\\p{L} _.]+$");
var s = "123 Московская Street.";
if (XRegExp.test(s, pattern)) {
console.log("Valid");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/3.2.0/xregexp-all.min.js"></script>
请注意 ^ [0-9 \\p {L} _ \\\\ 。] + $
匹配
^
- 开始of string [0-9 \\p {L} _\\。 +
- 一个或多个字符: 0-9
- ASCII数字 \\p {L}
- 字母
- 空间 _
- 下划线。
- 一个点(在一个字符类中,。
匹配一个文字点,无需转义)^
- start of string[0-9\\p{L} _\\.]+
- one or more chars tha are:
0-9
- ASCII digits\\p{L}
- letters
- space_
- an underscore .
- a dot (inside a character class, .
matches a literal dot, no need to escape)如果您还想包括以下条件:
If you want to also include the following conditions:
您可以将模式扩展到以下内容:
You may extend the pattern to the following:
var pattern = new XRegExp("^(?!.*\\bRiot\\b)[0-9\\p{L} _\\.]{3,16}$");
^^^^^^^^^^^^^^^^ ^^^^^^
其中 +
(1次或更多次出现)被替换为 {3,16}
限制量词( 3到16次出现)和(?!。* \\bRiot\\b)
如果有一个完整的单词,则负向前瞻将使比赛失败(由于 \\b
单词边界) Riot
是字符串内的任何位置(或行,因为。
匹配任何char但是换行符。)
where +
(1 or more occurrences) is replaced with {3,16}
limiting quantifier (3 to 16 occurrences) and (?!.*\\bRiot\\b)
negative lookahead will fail the match if there is a whole word (due to \\b
word boundaries) Riot
is anywhere inside the string (or line, since .
matches any char but line break chars).