解释@mentions的正则表达式是如何工作的、它的组件以及如何修改它



表达式如下:
(?:^|[ \p{Ps}\p{Pi}"'])([someText])([_\p{L}\p{N}]*)$,
我在CKeditor的源代码中发现了这一点,我想了解它的作用。这是用于提及,目前这个编辑器中的提及不支持空格,我认为这是因为这个正则表达式因为:

const str1 ="name"
const str2 = "name2 "
const testString = (str)=>{
const expression = `(?:^|[ \p{Ps}\p{Pi}"'])([${str}])([_\p{L}\p{N}]*)$`
const tester = new RegExp(expression, "u") 
console.log(tester.test(str))
}
testString(str1) // logs true
testString(str2) // logs false

因此,除了理解正则表达式之外,我的主要目标是如何修改它,以便我可以提到用户的名字和用户名的含义,使这个支持空间。


谢谢。
/
(?:^|[ \p{Ps}\p{Pi}"'])([someText])([_\p{L}\p{N}]*)$
/
gm
Non-capturing group (?:^|[ \p{Ps}\p{Pi}"'])
1st Alternative ^
^ asserts position at start of a line
2nd Alternative [ \p{Ps}\p{Pi}"']
Match a single character present in the list below [ \p{Ps}\p{Pi}"']
matches the character  with index 3210 (2016 or 408) literally (case sensitive)
\ matches the character  with index 9210 (5C16 or 1348) literally (case sensitive)
p{Ps}
matches a single character in the list p{Ps} (case sensitive)
\ matches the character  with index 9210 (5C16 or 1348) literally (case sensitive)
p{Pi}
matches a single character in the list p{Pi} (case sensitive)
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
' matches the character ' with index 3910 (2716 or 478) literally (case sensitive)
1st Capturing Group ([someText])
Match a single character present in the list below [someText]
someText
matches a single character in the list someTxt (case sensitive)
2nd Capturing Group ([_\p{L}\p{N}]*)
Match a single character present in the list below [_\p{L}\p{N}]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
_ matches the character _ with index 9510 (5F16 or 1378) literally (case sensitive)
\ matches the character  with index 9210 (5C16 or 1348) literally (case sensitive)
p{L}
matches a single character in the list p{L} (case sensitive)
\ matches the character  with index 9210 (5C16 or 1348) literally (case sensitive)
p{N}
matches a single character in the list p{N} (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

你可以在这里更好地阅读:

https://regex101.com/r/siYpVS/1

最新更新