javascript - JS/node.js中匹配字符的行号

标签 javascript regex node.js

是否可以在Javascript或node.js中找到多行输入(例如文件)的正则表达式匹配字符的行号?

最佳答案

是的,有一个半笨拙的解决方法。

http://jsfiddle.net/tylermwashburn/rbbqn/

var string = "This\nstring\nhas\nmultiple\nlines.",
    astring = string.split('\n'),
    match = /has/, foundon;

Array.each(astring, function (line, number) {
    if (match.exec(line))
        foundon = number + 1;
});

关于javascript - JS/node.js中匹配字符的行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6946466/

相关文章:

node.js - e.preventDefault() 不是函数 NextJS/React

node.js - 连续迭代 mongodb 游标(在移动到下一个文档之前等待回调)

Javascript 附加到 onClick 事件

javascript - 关于 AngularJs 中的 ngCookie

r - 在 R 中反转正则表达式

javascript - angular.fromJson(data.Body) 与 JSON.parse(data.Body)

javascript - jQuery 下拉菜单 : not showing one item

javascript - 删除 :checked . 映射函数中重复的 val() 时出现问题

php - 将正则表达式从 ruby​​ 转换为 PHP

ruby - Rails4 : model validates "format with" regex for slug column