简体   繁体   中英

Regex to find exact word containing ampersand match in a string

I want to find if "&1" (Exact) exists in a string or not with the help of Regular Expression. But I am not getting the correct result.

/\b\&1\b/i.test("Order Date is &1")

I am getting false result here. What is wrong?

简单一点:

 console.log(/(^|\\W)&1(\\W|$)/.test("Order Date is &1")) 

Since & is not alphanumeric, there is no "break" of an alphanumerical sequence between a space and & . So you may need the opposite of \\b , which is \\B :

 console.log(/\\B&1\\b/.test("Order Date is &1")); // true console.log(/\\B&1\\b/.test("Order Date is&1")); // false console.log(/\\B&1\\b/.test("&1 is the Order Date")); // true 

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