Limit javascript regex length

I have a pretty complicated regex that matches multiple lines. As part of this requirement, I need to replace part of the corresponding text with a truncated version. Using the backlink, I get the text, but how to use "string" .replace () to truncate it, for example. only the first 10 characters? Since there can be several matches in a string, I don’t want to manually extract and truncate the text.

+3
source share
1 answer

In Javascript 1.3, you can pass a function as a replacement argument:

s = s.replace(/someregularexpression/g, function(x){ return x.substr(0, 10); });

(source)

+2
source

Source: https://habr.com/ru/post/1787304/


All Articles