简体   繁体   中英

Javascript regex for tag in comments

I've been working on a web app in which users can comment and reply to comments, this uses a tagging system. The users are being tagged by their name which can contain more words so I've decided to mark the takes like this:

&&John Doe&&

So a comment might look like this:

&&John Doe&&, are you sure that &&Alice Johnson&& is gone?

I'm trying to write a regex to match use in a string.replace() javascript function, so the regex must match every single tag in the string.

So far I have:

^&&.+{2, 64}&&$

This isn't working so I'm sure something is wrong, in case you didn't understand what I meant, the regex from above is supposed to match strings like this:

&&anythingbetween2and64charslong&&.

Thanks in advance!

(.*?)&& means "everything until &&" :

 var before = document.getElementById("before"); var after = document.getElementById("after"); var re = /&&(.*?)&&/g, b = "<b>$1</b>"; after.innerHTML = before.textContent.replace(re, b); 
 <p id="before">&&John Doe&&, are you sure that &&Alice Johnson&& is gone?</p> <p id="after"></p> 

try &{2}.{2,64}&{2}

if you want to get the match in between add parentheses for the match group

&{2}(.{2,64})&{2}

right now your are only checking strings where the entire line matches

the ^ character means beginning of line the $ character means end of line \\A means beginning of entire string \\Z means end of entire string

Here's what you need:

str.match(/&&.{2,64}?&&/g)
  • you need to remove ^ and $ from the start and the end since they match the start and the end of the string.
  • add a /g flag at the end so all the matches will be matched
  • ? after the {} makes the match non-greedy, so it will match the shortest possible string between "&&" instead of the longest (will give you "&&John Doe&&" instead of "&&John Doe&&, are you sure that &&Alice Johnson&&")

Read up on greediness: Repetition with Star and Plus

This regex will match any Unicode letter between && signs:

str.match(/\&\&[\p{L}\p{N}]+(?:\s+[\p{L}\p{N}]+)*\&\&/g);

Here,

\\p{L} --> Any unicode letter, the names can be any language and letter
\\p{N} --> Any unicode digit
[\\p{L}\\p{N}]+ --> A word constructed with unicode letters or digits
\\s+ --> Gaps between words, max 3 length
[\\p{L}\\p{N}]+(?:\\s+[\\p{L}\\p{N}]+)* --> All word groups

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM