سعيد
سعيد

Reputation: 1764

explain how this regex for @mentions work and its components and how to modify it

here is the expression:
(?:^|[ \\p{Ps}\\p{Pi}\"'])([someText])([_\\p{L}\\p{N}]*)$ ,
I found this in the source code for CKeditor and I want to understand what it does . this is used for mentions , and currently the mentions in this ckeditor don't support spaces and I think it because of this regex expression because :

  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

so my main objectif with this question other than understanding the regex expression is how can I modify it so that I can mention users with their name and user name meaning making this support space .
thanks.

Upvotes: 0

Views: 729

Answers (1)

ZiTAL
ZiTAL

Reputation: 3581

/
(?:^|[ \\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)

You can read it better here:

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

Upvotes: 1

Related Questions