【问题标题】:JavaScript startsWith using regex & offsetJavaScript 开始使用正则表达式和偏移量
【发布时间】:2019-05-27 16:44:54
【问题描述】:

我正在做一些字符串解析并想使用正则表达式。我正在遍历字符串,并希望使用正则表达式和偏移量应用类似“startsWith”的内容,如果找到则返回匹配项,否则返回 null。在伪javascript中:

function startsWith(string, regex, offset) {
    if (regex_matches_at_offset) {
        return match;
    } else {
        return null;
    }
}

一个简单直接的解决方案是先应用子字符串然后匹配。但我想要使用正则表达式的“startsWith”之类的东西。

如果是字符串而不是正则表达式,我会选择startsWith

function startsWith(string, other_string, offset) {
    let starts_with=s.startsWith(other_string, offset); // startsWith from position offset onwards, as other_string has fixed length the "match" is also known
    if (starts_with) {
        return other_string; // other_string = match
    } else {
        return null;
    }
}

但是对于正则表达式,我当前的解决方案(仅用于测试目的)看起来像:

function startsWith(string, regex, offset) {
    let end_part=s.substring(offset); // Substring, performance issue
    let match=end_part.match(regex); // Match it as we need the match
    if (match.length === 0) {
        return null;
    } else {
        match=match[0]; // Only care about 1st match
    }
    if (end_part.startsWith(match)) { // Check if match already starts at first position
        return match;
    } else {
        return null;
    }
}

这不是很令人满意,因为存在明显的问题(复制大部分字符串,执行两次正则表达式搜索......

预期:

  1. 从给定位置开始在字符串中查找正则表达式,不仅是布尔值,还要匹配
  2. 高性能(例如,不要复制整个字符串)

例子:

startsWith("Hello world !", /h/i, 0); // "H"
startsWith("Hello world !", /wor?/i, 6); // "wor"
startsWith("Hello world !", /wor?/i, 10); // null
startsWith("Hello world !", /z/i, 0); // null

【问题讨论】:

    标签: javascript regex string performance regex-group


    【解决方案1】:

    一种方法是根据作为字符串传递的偏移量和模式构建正则表达式

    • 这里正则表达式的初始部分使用. 匹配任何字符到偏移量
    • 在捕获的组中第二次使用传递的模式仅返回所需的匹配项

    let startsWith = (str, reg, offset) =>{
      let regex = `^.{${0,offset}}(${reg})`
      let final = new RegExp(regex,'i')
      let found = str.match(final)
      console.log(found ? found[1] : found)
    }
    
    startsWith("Hello world !", 'h', 0); // "H"
    startsWith("Hello world !", 'wor?', 6); // "wor"
    startsWith("Hello world !", 'wor?', 10); // null
    startsWith("Hello world !", 'z', 0); // null

    正如@mark 提到的,如果我们希望将正则表达式作为参数传递给函数,我们可以使用source 属性

    let startsWith = (str, reg, offset) =>{
      let regex = `^.{${0,offset}}(${reg.source})`
      let final = new RegExp(regex,'i')
      let found = str.match(final)
      console.log(found ? found[1] : found)
    }
    
    startsWith("Hello world !", /h/i, 0); // "H"
    startsWith("Hello world !", /wor?/i, 6); // "wor"
    startsWith("Hello world !", /wor?/i, 10); // null
    startsWith("Hello world !", /z/i, 0); // null

    【讨论】:

    • 您可以考虑使用正则表达式属性source 提取一个字符串,该字符串允许函数接受正则表达式而不是字符串。
    • @MarkMeyer 不知道让我阅读和更新,感谢您提供信息
    • 它似乎适用于这些示例,但我不确定它是否会产生困难的边缘情况。
    • 再想一想,有没有一种方法不会产生 RegEx 编译的成本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 2010-10-16
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多