简体   繁体   English

str.replace(),获取匹配数或替代的最后一个元素?

[英]str.replace(), get number of matches, or last element for substitution?

I have a string that uses placeholders that I'm trying to substitute with the result of function.To give an idea: 我有一个使用占位符的字符串,我试图用占位符替换为函数的结果。

sometext = "%number% text text %number%"replace(/%\w+%/g, function(parm) {
       return 'customtext';
}

What I would like to know is there a way to get the number of matches? 我想知道有什么办法可以增加比赛次数吗? I need that number so I can check if the current running function is substituting the last element, and if so, return something else.Something like this in the body of function 我需要这个数字,以便可以检查当前正在运行的函数是否正在替换最后一个元素,如果是,则返回其他内容。

if(lastElement) {
   return 'something else';
}

sometext = "%number% text text %number%"replace(/%\w+%/g, function(parm) {
       return 'customtext';

You can't directly get the number of matches in the function. 您无法直接获取函数中的匹配数。 It's not provided. 没有提供。

I'd simply count it before and count the function calls during the replacement : 我只是简单地计算一下,并在替换期间计算函数调用:

var i = 0, n = str.split(/%\w+%/).length-1;
var sometext = str.replace(/%\w+%/g, function() {
   if (++i==n) return 'last text';
   else return 'not last text';
});

I think you want to use javascript's .match() function. 我认为您想使用javascript的.match()函数。 You give it a regular expression, and it returns an array of matches. 您给它一个正则表达式,它返回一个匹配数组。 You can then get the length of the array. 然后,您可以获取数组的长度。

See: 看到:

http://www.w3schools.com/jsref/jsref_match.asp http://www.w3schools.com/jsref/jsref_match.asp

This will give the number of matches: 这将给出匹配的数量:

var last = "%number% text text %number%".match(/%\w+%/g).length;

Then put your 'if' in the callback that you are using and keep track with a counter of how many times the callback has been executed comparing to the length gleaned above and voila. 然后将您的“ if”放入正在使用的回调中,并跟踪一个计数器,该计数器将回调执行了多少次(与上面收集的长度和查询结果相比)。

Cheers. 干杯。

First, find the number of matches: 首先,找到匹配数:

var matchCount = haystack.match(needle);

Then, count down the number of remaining matches on each replace: 然后,记下每次替换的剩余匹配数:

haystack.replace(needle, function(param) {
    matchCount--;
    if( matchCount == 0 ) {
        return 'Last Element';
    }
    return 'Not Last Element';
});

The combined code would look something like this: 合并的代码如下所示:

var haystack = "%number% text text %number%";
var needle = new RegExp("%\w+%", "g");

var matchCount = haystack.match(needle);
haystack.replace(needle, function(param) {
    matchCount--;
    if( matchCount == 0 ) {
        return 'Last Element';
    }
    return 'Not Last Element';
});

And can be shortened to a function similar to this: 并且可以简化为类似于以下的功能:

function customReplace(haystack, needle) {
    var count = haystack.match(needle);
    haystack.replace(needle, function() {
        return (--count ? 'notLast' : 'isLast');
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM