是否有一种方法来检索(开始)字符的位置在一个正则匹配()在Javascript的结果字符串?
Exec返回一个带有index属性的对象:
Var match = /bar/.exec("foobar"); If (match) { Console.log ("match found at " + match.index); }
对于多个匹配:
Var re = /bar/g, STR = "foobarfoobar"; While ((match = re.exec(str)) != null) { Console.log ("match found at " + match.index); }
您可以使用String对象的搜索方法。这将只适用于第一个匹配,但在其他情况下将完成您所描述的工作。例如:
"How are you?".search(/are/);
// 4
这是我想到的:
//查找引用文本的开始和结束位置 //双引号或单引号,支持转义字符,如\" \' Var STR = "这是一个带引号的字符串,因为你可以'读'"; var型 = /'((?:\\.|[^'])*)'|"((?:\\.|[^"])*)"/ igm; While (match = pat .exec(str)) { console.log(匹配。index + ' ' + pat . lastindex); }
这个成员fn返回String对象中输入单词的以0为基础的位置数组(如果有的话)
String.prototype.matching_positions = function( _word, _case_sensitive, _whole_words, _multiline )
{
/*besides '_word' param, others are flags (0|1)*/
var _match_pattern = "g"+(_case_sensitive?"i":"")+(_multiline?"m":"") ;
var _bound = _whole_words ? "\\b" : "" ;
var _re = new RegExp( _bound+_word+_bound, _match_pattern );
var _pos = [], _chunk, _index = 0 ;
while( true )
{
_chunk = _re.exec( this ) ;
if ( _chunk == null ) break ;
_pos.push( _chunk['index'] ) ;
_re.lastIndex = _chunk['index']+1 ;
}
return _pos ;
}
现在试试
var _sentence = "What do doers want ? What do doers need ?" ;
var _word = "do" ;
console.log( _sentence.matching_positions( _word, 1, 0, 0 ) );
console.log( _sentence.matching_positions( _word, 1, 1, 0 ) );
您也可以输入正则表达式:
var _second = "z^2+2z-1" ;
console.log( _second.matching_positions( "[0-9]\z+", 0, 0, 0 ) );
这里我们得到了线性项的位置指标。
来自developer.mozilla.org文档的String .match()方法:
返回的数组有一个额外的输入属性,该属性包含 被解析的原始字符串。此外,它还有一个索引 属性中匹配的从零开始的索引 字符串。
当处理一个非全局的正则表达式(即,在你的正则表达式上没有g标志)时,.match()返回的值有一个index属性…你要做的就是进入它。
var index = str.match(/regex/).index;
下面是一个例子,展示了它的工作原理:
Var STR = '我的字符串这里'; Var index = str.match(/here/).index; console.log(指数);// <- 10
我已经成功地测试了IE5。
var str = "The rain in SPAIN stays mainly in the plain";
function searchIndex(str, searchValue, isCaseSensitive) {
var modifiers = isCaseSensitive ? 'gi' : 'g';
var regExpValue = new RegExp(searchValue, modifiers);
var matches = [];
var startIndex = 0;
var arr = str.match(regExpValue);
[].forEach.call(arr, function(element) {
startIndex = str.indexOf(element, startIndex);
matches.push(startIndex++);
});
return matches;
}
console.log(searchIndex(str, 'ain', true));
这是我最近发现的一个很酷的功能,我在主机上尝试了一下,似乎很管用:
var text = "border-bottom-left-radius";
var newText = text.replace(/-/g,function(match, index){
return " " + index + " ";
});
返回:"border 6 bottom 13 left 18 radius"
看来这就是你要找的东西。
function trimRegex(str, regex){
return str.substr(str.match(regex).index).split('').reverse().join('').substr(str.match(regex).index).split('').reverse().join('');
}
let test = '||ab||cd||';
trimRegex(test, /[^|]/);
console.log(test); //output: ab||cd
or
function trimChar(str, trim, req){
let regex = new RegExp('[^'+trim+']');
return str.substr(str.match(regex).index).split('').reverse().join('').substr(str.match(regex).index).split('').reverse().join('');
}
let test = '||ab||cd||';
trimChar(test, '|');
console.log(test); //output: ab||cd
在现代浏览器中,您可以使用string.matchAll()来实现这一点。
与RegExp.exec()相比,这种方法的优点是它不依赖于正则表达式是有状态的,就像@Gumbo的答案中那样。
让regexp = /bar/g; 让STR = 'foobarfoobar'; let matches =[…str.matchAll(regexp)]; matches.forEach((match) => { Console.log ("match found at " + match.index); });
我恐怕之前的答案(基于exec)似乎不工作的情况下,你的正则表达式匹配宽度0。例如(注意:/\b/g是应该找到所有单词边界的正则表达式):
Var re = /\b/g, STR = "hello world"; Var guard = 10; While ((match = re.exec(str)) != null) { Console.log ("match found at " + match.index); If (guard—< 0){ 控制台。错误(“检测到无限循环”) 打破; } }
可以尝试通过让正则表达式匹配至少1个字符来修复这个问题,但这远远不够理想(并且意味着您必须手动在字符串末尾添加索引)。
Var re = /\b /g, STR = "hello world"; Var guard = 10; While ((match = re.exec(str)) != null) { Console.log ("match found at " + match.index); If (guard—< 0){ 控制台。错误(“检测到无限循环”) 打破; } }
一个更好的解决方案(它只适用于较新的浏览器/需要在较旧的/IE版本上进行填充)是使用String.prototype.matchAll()
Var re = /\b/g, STR = "hello world"; console.log(Array.from(str.matchAll(re)).map(match => match.index))
解释:
String.prototype.matchAll()需要一个全局正则表达式(带有全局标志设置g的正则表达式)。然后返回一个迭代器。为了遍历和映射()迭代器,它必须转换为一个数组(这正是array .from()所做的)。与RegExp.prototype.exec()的结果类似,生成的元素根据规范有一个.index字段。
请参阅String.prototype.matchAll()和Array.from() MDN页面了解浏览器支持和填充选项。
编辑:深入挖掘一个所有浏览器都支持的解决方案
RegExp.prototype.exec()的问题是它更新了regex上的lastIndex指针,并且下次开始从以前找到的lastIndex开始搜索。
Var re = /l/g, STR = "hello world"; console.log (re.lastIndex) re.exec (str) console.log (re.lastIndex) re.exec (str) console.log (re.lastIndex) re.exec (str) console.log (re.lastIndex)
只要正则表达式匹配实际上有宽度,这就很有效。如果使用0 width正则表达式,这个指针不会增加,所以你得到了无限循环(注意:/(?=l)/g是l的前向——它在l之前匹配0-width字符串。所以它在第一次调用exec()时正确地去到索引2,然后保持在那里:
Var re = /(?=l)/g, STR = "hello world"; console.log (re.lastIndex) re.exec (str) console.log (re.lastIndex) re.exec (str) console.log (re.lastIndex) re.exec (str) console.log (re.lastIndex)
The solution (that is less nice than matchAll(), but should work on all browsers) therefore is to manually increase the lastIndex if the match width is 0 (which may be checked in different ways) var re = /\b/g, str = "hello world"; while ((match = re.exec(str)) != null) { console.log("match found at " + match.index); // alternative: if (match.index == re.lastIndex) { if (match[0].length == 0) { // we need to increase lastIndex -- this location was already matched, // we don't want to match it again (and get into an infinite loop) re.lastIndex++ } }
我很幸运地使用了这个基于matchAll的单行解决方案(我的用例需要一个字符串位置数组)
let regexp = /bar/g;
let str = 'foobarfoobar';
let matchIndices = Array.from(str.matchAll(regexp)).map(x => x.index);
console.log(matchIndices)
输出:[3,9]
推荐文章
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- PowerShell和-contains操作符
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类
- 访问Handlebars.js每次循环范围之外的变量
- 如何用JavaScript截屏一个div ?
- 如何为其他域设置cookie
- 如何减去日期/时间在JavaScript?
- 如何检测“搜索”HTML5输入的清除?
- 字符串中的单词大写
- 返回一个正则表达式匹配()在Javascript的位置?
- Ajax成功事件不工作
- 提取正则表达式匹配的一部分
- 为什么JavaScript中弃用arguments.callee.caller属性?