为什么新的RegEx无法正常工作

时间:2017-10-04 07:31:22

标签: javascript

出于某种原因......

我何时尝试从Object.keys获取replace.letters并使用Object.keys new RegExp joined中的| ...

new RegExp只识别部分Object.keys但不是全部。我需要RegEx动态创建自己。

如果我放置一个静态RegExp ......它的效果非常好。

  1. 演示无法正常运作

  2. 演示效果很好but I have to force the Regex to know what to look for

  3. replace = {
      letters: {
        a: {
          after: ["til"]
        },
        b: {
          because: ["bec"]
        },
        c: {
          cool: ["chill"]
        },
        e: {
          energy: ["en"]
        },
        h: {
          light: ["look"]
        },
        i: {
          keen: ["ok"]
        },
        r: {
          roll: ["rock"]
        },
        s: {
          ship: ["skip"]
        },
        t: {
          trip: ["tip"]
        }
      }
    }
    
    sentence = [
      "If you want a cookie, eat your dinner.",
      "As soon as you enter the house, change clean your room and mop the floor.",
      "So long as I'm king, I will ensure your safty.",
      "Change the curtains, after house is painted.",
      "Change the curtains, by the time that we are headed.",
      "Assuming that you are a good person, hold my beer.",
      "Reject the offer, even if I'm not there.",
      "Reject the offer, zoom I'm not there.",
      "By the time the bus gets here, the show will be over with.",
      "Choose a random number.",
      "Try not to mess this, that and those up.",
      "Change a random number, pick a color, and put it in jason's house.",
      "Zoom, fix and lower the bar.",
      "Don't change the house.",
      "Create a playground.",
      "While you were gone, I changed the lockes",
      "Before I stop the car, can you get my wallet."
    ]
    let objects_letters = Object.keys(replace.letters).join('|')
    let letters_RegEx = new RegExp('^(' + objects_letters + ')', 'gi')
    console.log(letters_RegEx)//This isn't working properly.
    
    for (var i = 0; i < sentence.length; i++) {
      let $this = sentence[i]
      let commaKey = /,/g.test($this)
      let if_While_Key = /^(If|While)/gi.test($this)
    
    
      //Here is the problem
      let letterKey = letters_RegEx.test($this)
      //Here is the problem
    
    
      if (commaKey) {
        if (if_While_Key) {}
        if (letterKey) {
          $('body').append($this + '<br>');
        } else {}
      } else {}
    
    
    
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    replace = {
      letters: {
        a: {
          after: ["til"]
        },
        b: {
          because: ["bec"]
        },
        c: {
          cool: ["chill"]
        },
        e: {
          energy: ["en"]
        },
        h: {
          light: ["look"]
        },
        i: {
          keen: ["ok"]
        },
        r: {
          roll: ["rock"]
        },
        s: {
          ship: ["skip"]
        },
        t: {
          trip: ["tip"]
        }
      }
    }
    
    sentence = [
      "If you want a cookie, eat your dinner.",
      "As soon as you enter the house, change clean your room and mop the floor.",
      "So long as I'm king, I will ensure your safty.",
      "Change the curtains, after house is painted.",
      "Change the curtains, by the time that we are headed.",
      "Assuming that you are a good person, hold my beer.",
      "Reject the offer, even if I'm not there.",
      "Reject the offer, zoom I'm not there.",
      "By the time the bus gets here, the show will be over with.",
      "Choose a random number.",
      "Try not to mess this, that and those up.",
      "Change a random number, pick a color, and put it in jason's house.",
      "Zoom, fix and lower the bar.",
      "Don't change the house.",
      "Create a playground.",
      "While you were gone, I changed the lockes",
      "Before I stop the car, can you get my wallet."
    ]
    let objects_letters = Object.keys(replace.letters).join('|')
    let letters_RegEx = new RegExp('^(' + objects_letters + ')', 'gi')
    console.log(letters_RegEx)//This isn't working properly.
    
    for (var i = 0; i < sentence.length; i++) {
      let $this = sentence[i]
      let commaKey = /,/g.test($this)
      let if_While_Key = /^(If|While)/gi.test($this)
    
    
      //Here is the problem
      let letterKey = /^(a|b|c|e|h|i|r|s|t)/gi.test($this)
      //Here is the problem
    
    
      if (commaKey) {
        if (if_While_Key) {}
        if (letterKey) {
          $('body').append($this + '<br>');
        } else {}
      } else {}
    
    
    
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

2 个答案:

答案 0 :(得分:1)

您的问题是RegExp Flags

更改

let letters_RegEx = new RegExp('^(' + objects_letters + ')', 'gi')

let letters_RegEx = new RegExp('^(' + objects_letters + ')', 'i')


replace = {
  letters: {
    a: {
      after: ["til"]
    },
    b: {
      because: ["bec"]
    },
    c: {
      cool: ["chill"]
    },
    e: {
      energy: ["en"]
    },
    h: {
      light: ["look"]
    },
    i: {
      keen: ["ok"]
    },
    r: {
      roll: ["rock"]
    },
    s: {
      ship: ["skip"]
    },
    t: {
      trip: ["tip"]
    }
  }
}

sentence = [
  "If you want a cookie, eat your dinner.",
  "As soon as you enter the house, change clean your room and mop the floor.",
  "So long as I'm king, I will ensure your safty.",
  "Change the curtains, after house is painted.",
  "Change the curtains, by the time that we are headed.",
  "Assuming that you are a good person, hold my beer.",
  "Reject the offer, even if I'm not there.",
  "Reject the offer, zoom I'm not there.",
  "By the time the bus gets here, the show will be over with.",
  "Choose a random number.",
  "Try not to mess this, that and those up.",
  "Change a random number, pick a color, and put it in jason's house.",
  "Zoom, fix and lower the bar.",
  "Don't change the house.",
  "Create a playground.",
  "While you were gone, I changed the lockes",
  "Before I stop the car, can you get my wallet."
]
let objects_letters = Object.keys(replace.letters).join('|')
let letters_RegEx = new RegExp('^(' + objects_letters + ')', 'i')
console.log(letters_RegEx)//This isn't working properly.

for (var i = 0; i < sentence.length; i++) {
  let $this = sentence[i]
  let commaKey = /,/g.test($this)
  let if_While_Key = /^(If|While)/gi.test($this)


  //Here is the problem
  let letterKey = letters_RegEx.test($this)
  //Here is the problem


  if (commaKey) {
    if (if_While_Key) {}
    if (letterKey) {
      $('body').append($this + '<br>');
    } else {}
  } else {}



}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

当包含在字符串(RegExp)中时,必须在特殊字符前加上\。

编辑:看起来您需要转义正则表达式中的所有特殊字符。我从Mathias Bynens

借用了escapeRegExp函数

请参阅以下更正后的摘录:

replace = {
  letters: {
    a: {
      after: ["til"]
    },
    b: {
      because: ["bec"]
    },
    c: {
      cool: ["chill"]
    },
    e: {
      energy: ["en"]
    },
    h: {
      light: ["look"]
    },
    i: {
      keen: ["ok"]
    },
    r: {
      roll: ["rock"]
    },
    s: {
      ship: ["skip"]
    },
    t: {
      trip: ["tip"]
    }
  }
}

sentence = [
  "If you want a cookie, eat your dinner.",
  "As soon as you enter the house, change clean your room and mop the floor.",
  "So long as I'm king, I will ensure your safty.",
  "Change the curtains, after house is painted.",
  "Change the curtains, by the time that we are headed.",
  "Assuming that you are a good person, hold my beer.",
  "Reject the offer, even if I'm not there.",
  "Reject the offer, zoom I'm not there.",
  "By the time the bus gets here, the show will be over with.",
  "Choose a random number.",
  "Try not to mess this, that and those up.",
  "Change a random number, pick a color, and put it in jason's house.",
  "Zoom, fix and lower the bar.",
  "Don't change the house.",
  "Create a playground.",
  "While you were gone, I changed the lockes",
  "Before I stop the car, can you get my wallet."
]
let objects_letters = Object.keys(replace.letters).join('\|')
let letters_RegEx = new RegExp(escapeRegExp('^(' + objects_letters + ')'), 'i')
console.log(letters_RegEx);

function escapeRegExp(text) {
  return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\$&');
}

for (var i = 0; i < sentence.length; i++) {
  let $this = sentence[i]
  let commaKey = /,/g.test($this)
  
  let if_While_Key = /^(If|While)/gi.test($this)

  //FIXED
  let letterKey = letters_RegEx.exec($this)

  if (commaKey) {
    if (if_While_Key) {}
    if (letterKey) {
      $('body').append($this + '<br>');
    } else {}
  } else {}



}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>