2017-10-04 108 views
1

出于某种原因...为什么不能正常工作

当过我尝试从replace.lettersObject.keys和使用Object.keys新的正则表达式一个new RegExpjoined通过| ...

new RegExp只承认一些Object.keys但不是全部。我需要RegEx来动态创建它自己。

如果我把一个静态的RegExp放在...它工作得很好。

  1. 演示不能正常工作

  2. 演示完美的作品but I have to force the Regex to know what to look for

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>

+0

是否使用grep或类似的命令行上的东西的工作? –

+0

我不熟悉'grep()' –

+0

我只想测试'true'或'false' –

回答

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>

0

如果包含在字符串中(RegExp),您必须在特殊字符前加\。

编辑:它看起来像你需要逃避正则表达式中的所有特殊字符。我借用escapeRegExp功能从Mathias Bynens

请参见下面的更正代码片段:

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>

+0

'缩放,固定和降低酒吧.'应该写成HTML –

+0

也不'不要改变该房子。“ –

+0

只有12句应写入HTML –