开发者

Token replacement in javascript

开发者 https://www.devze.com 2022-12-30 08:29 出处:网络
What I need to implement is to find patterns in a string , store matches replace the matches with unique tokens so that later on the token could be replaced by its match found earlier.

What I need to implement is to

find patterns in a string , store matches replace the matches with unique tokens so that later on the token could be replaced by its match found earlier.

To explain

for eample I have an array of patterns

paterns = [/Mr\.\s/,/Mrs\.\s/];


stringSubject =开发者_如何学运维 "Mr. john is an expert. mrs. john is no less. mr. watson know this very well";

after extracting matches it might look like (case insensitive match)

stringSubject = "{1} john is an expert. {2} john is no less. {3} watson know this very well";

and the tokens array might look like

tokens = ["Mr.","mr.","mrs."]

stringSubject = "{1} john is an expert. {3} john is no less. {2} watson know this very well";

// after processing stringSubject

tokens are replaced such that

stringSubject = "Mr. john is an expert. mrs. john is no less. mr. watson know this very well";

so that the original string is retrieved as it is even after performing case insensitive operation for matching patterns.

How can this be done with regex ?


How's this?

var stringSubject = "Mr. john is...",
    patterns = [/Mr\.\s/, /Mrs\.\s/],
    currentTokenIndex = 0,
    tokens = [/* this is where the matches will be stored */];

for (var i = -1, l = patterns.length; ++i < l;) {
    stringSubject = stringSubject.replace(patterns[i], function(match){
        tokens[currentTokenIndex] = match;
        return '{' + currentTokenIndex++ + '}';
    });
}

stringSubject; // <= "{0}john is..."

// Later:
stringSubject = stringSubject.replace(/\{(\d+?)\}/g, function(match, index){
    return tokens[index];
});

stringSubject; // <= "Mr. john is..."
0

精彩评论

暂无评论...
验证码 换一张
取 消