1

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!

3
  • Are there some edge cases you want to consider? Like & inside a name or escaping mechnisms? Otherwise just go by &&.{2,64}?&& Commented Feb 4, 2017 at 19:52
  • No, the usernames will contain only alphabetic characters, whitespaces and apostrophes. Commented Feb 4, 2017 at 19:54
  • Apparently, this works just fine, many thanks! Commented Feb 4, 2017 at 19:55

4 Answers 4

3

(.*?)&& 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>

Sign up to request clarification or add additional context in comments.

2 Comments

Don't use innerHTML as it'll destroy events and triggers regeneration of the DOM. Instead, use something like mark.js, e.g. new Mark(document.getElementById("before")).markRegExp(/&&(.*?)&&/g);
You are right @dude, I use it for demo purpose only, but it can be misleading indeed :-)
0

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

Comments

0

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

Comments

-1

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

13 Comments

Now this looks like a complete approach, thank you, sir!
Yeah, you know names are all from different kinds of languages so you have to use the unicode match-case for the regex pattern, updated it
This will only work with XRegExp. Native JS regex engine does not support Unicode property classes.
So @NelutuFona you gotta find a server-side approach, because I am sure you will face with this problem somehow
Yea, right now I need to buld a server-side approach for this(strictly for one name, used in URL), so basically I need a regex that will work with php 7 and will match any name, no shorter than 2 characters and no longer than 64
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.