There is a string str in which the user may be mentioned (for example, @username ).
I need to find out if there is a mention and, if there is one, frame it <span> .

  • one
    What did you have a problem with when solving this task, and what is the real question? - Regent
  • @Regent the problem is that the level of knowledge in js / jquery is almost equal to 0 (I specialize in back-end'e), but there is an urgent need to implement this task exactly on js / jquery - ilyaspark
  • Are there any restrictions on the username format? say spaces, characters? - cyadvert
  • @cyadvert mention will consist of one word, in which there will be only numbers and letters - ilyaspark

1 answer 1

Here's an option:

 var str = 'this is text with @thisUsername in it, and @theOther username'; $('#stringBefore').html(str); str = str.replace(/(@\w+)/gm, "<span>$1</span>"); $('#stringAfter').html(str); 
 span { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Before: <div id="stringBefore"></div> After: <div id="stringAfter"></div> 

  • Thank you for what you need! - ilyaspark