语境:
我正在改进自定义令牌解析引擎,并希望支持不同语言的字符、数字和空格字符。
目前,这适用于具有以下正则表达式的英文字符和数字
var pattern = /\{\{someText\(?(\d+)?\|?([\w\d\s%]+)?\)?[\s\S]+\}\}/;
// this will extract the number and text in a token like
'{{someText(20|Hello World)}}'.match(pattern);
//output
[20, 'Hello World']
但是上面的正则表达式无法解析其他语言的字符:
'{{someText(20|abcdèfg)}}'.match(pattern);
//output
[20, 'abcd']
我尝试过的正则表达式:
我尝试将 XRegexp 与下面的正则表达式一起使用,但看起来它不像我预期的那样工作。
var pattern = XRegExp(/\{\{customText\(?(\d+)?\|?([[\p{L}\p{N}_]\p{Nd}[\p{Z}\h\v]%]+)?\)?[\s\S]+\}\}/);
'{{someText(20|abcdèfg)}}'.match(pattern);
//output
[20, undefined]
对于新引擎,我也想支持其他语言的字符。这样文本中的
'{{someText(20|abcdèfg)}}'.match(pattern);
将产生一个输出
[20, abcdèfg]
这个标记的格式总是像 {{someText(number|'The actual text')}}