Good day! I have a regular season of this kind:

/((^|[\s\.\,\;])(text)($|[\s\.\,\;])|(^|[\s\.\,\;])(textf)($|[\s\.\,\;])|(^|[\s\.\,\;])(texts)($|[\s\.\,\;])|(^|[\s\.\,\;])(textt)($|[\s\.\,\;]))/gi 

It consists of almost identical blocks of the form (^|[\s\.\,\;])(text)($|[\s\.\,\;])

which differ only in the forms of the desired word. The regular looks for all occurrences of the word text in various variations (text, textf, texts, textt).

Here is how I use it:

 'textf some words texts another words. Textt bla-bla-bla'.replace(/((^|[\s\.\,\;])(textf)($|[\s\.\,\;])|(^|[\s\.\,\;])(texts)($|[\s\.\,\;])|(^|[\s\.\,\;])(textt)($|[\s\.\,\;]))/gi, function() { console.log('"' + arguments[0] + '"'); return '<a>' + arguments[0] + '</a>'; }) 

console.log(arguments[90])

displays this:

"textf"
"texts"
"Textt"

The very same line after the replacement looks like this:

 <a>textf </a> some words<a> texts </a>another words.<a> Textt </a>bla-bla-bla 

That is, as you can see the matches found by the regular, include spaces with commas, which in theory should be word boundaries and not participate in the replacement. Obviously, I somehow incorrectly made a regular schedule. I would like the text to be as follows:

 <a>textf</a> some words<a>texts</a> another words.<a>Textt</a> bla-bla-bla 

Can you please tell me how to change the regular season to achieve this?

  • Why is a regular season so complicated? It was possible to write /(^ | [\s\.\,\;]) (text | textf | texts | textt) ($ | [\s\.\,\;])/gi - Pavel Mayorov
  • It could have been even simpler if the words consist of Latin characters: \b(?:textf|texts|textt)\b - Visman
  • @PavelMayorov I don't do the regulars. I feed some text to the library, and it gives me a regular list on it. - Pupkin
  • @Visman text is Cyrillic, so \ b does not fit - Pupkin
  • @Pupkin in this case the problem is in the library. - Pavel Mayorov

3 answers 3

The problem is not regular, but that is used for the substitution.

If you refer to the help for the replace function, you can see which parameters are passed to the function passed by the second argument.

  1. match - the part of the string that satisfies the regular expression
  2. p1, p2, ... are the values ​​of specific groups specified in the regular expression. The number of these parameters corresponds to the number of groups.
  3. offset - offset of the part of the string that satisfies the regular expression
  4. string - The entire considered string.

Since many groups are used, you need to substitute specific

 var result = 'textf some words texts another words. Textt bla-bla-bla'.replace(/((^|[\s\.\,\;])(textf)($|[\s\.\,\;])|(^|[\s\.\,\;])(texts)($|[\s\.\,\;])|(^|[\s\.\,\;])(textt)($|[\s\.\,\;]))/gi, function(...[, , , $1, , , $2, , , $3]) { // прСдставим, Ρ‡Ρ‚ΠΎ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Ρ‹ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Ρ‹ Π² массивС ΠΈ возьмСм элСмСнты с индСксами 3,6,9 return '<a>' + ($1 || $2 || $3) + '</a>'; }); console.log('result:', result); 

You can learn more about the constructions in the code here:

Or so:

 var result = 'textf some words texts another words. Textt bla-bla-bla'.replace(/((^|[\s\.\,\;])(textf)($|[\s\.\,\;])|(^|[\s\.\,\;])(texts)($|[\s\.\,\;])|(^|[\s\.\,\;])(textt)($|[\s\.\,\;]))/gi, function() { // прСдставим, Ρ‡Ρ‚ΠΎ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Ρ‹ ΠΏΠ΅Ρ€Π΅Π΄Π°Π½Ρ‹ Π² массивС ΠΈ возьмСм элСмСнты с индСксами 3,6,9 return '<a>' + (arguments[3] || arguments[6] || arguments[9]) + '</a>'; }); console.log('result:', result); 

  • Thank you very much, but unfortunately I did not understand how it works. More precisely, what is the ellipsis and a bunch of commas in the function arguments? - Pupkin
  • @Pupkin, it's just a more compact form. - Grundy
  • @Pupkin, added a description of the parameters in the answer. - Grundy
  • @Grundy, you have a script right here that gives an error. - Visman
  • @Visman, browser dependent. rest parameters and destructuring assignment are not implemented everywhere. It will not work exactly in IE - Grundy

Here is the solution for Russian and Latin letters (including digits and underscore, as I use \w )

 var text = 'Мама ΠΌΡ‹Π»Π° Ρ€Π°ΠΌΡƒ, Π° Π Π°ΠΌΡƒΠΈΠ» Π² ΠΊΠΈΠ½ΠΎ Ρ…ΠΎΠ΄ΠΈΠ».'; var res = text.replace(/(^|[^\wΠ°-яё])(ΠΌΠ°ΠΌΠ°|Ρ€Π°ΠΌΡƒ)(?![\wΠ°-яё])/gi, function() { return arguments[1] + '<a>' + arguments[2] + '</a>'; }) console.log(res); 

The character in front of the word will have to be captured, since js cannot look back and return it through a function ( arguments[1] variable arguments[1] ). The captured word itself is in arguments[2] .

    Try this solution here:

      ([T,t]ext[f|s|t]?) 
    • the problem is that words here can be absolutely any in any (from the point of view of the Russian language) word forms. text is just an example. - Pupkin