简体   繁体   中英

Regex Not Matching Similar String

Good day you all, please I need assistance with the regex in the code snippet below. Does anyone know why it is returning false and not true?

Thank you.

 console.log(/(?=.*\b\-100\/\-025\*180).+/gi.test("-1.00/-0.25*180"))

So it appears the problem is with the regex being too rigid. I am not sure what that means. But I found the solution after a bit of try and error using regex101.com.

So I was building a regex pattern from the search string "-1.00/-0.25*180" so when building the regex pattern, I had to discard the first character on the search string then escape special characters in the remaining characters and enclose all in a capturing group.

See the working examples below.

Regex101.com was extremely helpful in understanding the problem and thank you @mplungjan for the reference.

 // Testing against whole text console.log(/(?=.*\b(1\.00\/\-0\.25\*180)).+/gi.test("+1.00/-0.25*180")) // Testing against whole text console.log(/(?=.*\b(1\.00\/\-0\.25)).+/gi.test("+1.00/-0.25")) // Testing against whole text console.log(/(?=.*\b(1\.00\/\-0\.)).+/gi.test("+1.00/-0.")) // Testing against whole text console.log(/(?=.*\b(1\.00)).+/gi.test("+1.00")) // Testing against part of text console.log(/(?=.*\b(1\.00)).+/gi.test("+1.00/-0.25*180"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM