简体   繁体   中英

Javascript Regular Expression for whole word match which may contains special characters also

I have to find matches of search terms in a document of text. I am coding in Javascript. I used \\b for whole word match. But I facing one problem. The search Term will contain some special characters also.

example one:

Text: This article contains information on the Installment Loan-IL Profile which allows the teammate to view the details of a client's Installment Loan IL.

SearchTerm: " IL "

and Regex Exp I used is "\\b(IL)\\b" . It is working fine


example two:

Text:

The online New Account Center (NAC) is an automated way for prospects and clients to open consumer checking, savings, and money market accounts online through..

SearchTerm: " New Account Center(NAC) "

and Regex Exp I used is "\\b(New Account Center\\\\(NAC\\\\))\\b" . It is not working because of the ) in the end.

Please help me with a Regular expression which can accommodate both scenarios.

Try with this:

/\b(IL\b|New\s*Account\s*Center\s*\(NAC\))/g

If it's a single word without any whitespaces/delimeter , you can go ahead to include \\b both at the front and at the back , else you use the above format.

You need to escape the white spaces as above.

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