Dollar Sign "\$" in Regular Expressions with word boundaries "\b" (PHP / JavaScript) -


i aware issue involving dollar sign "$" in regex (here: either in php , javascript) has been discussed numerous times before: yes, know need add backslash "\" in front of (depending on string processing two), correct way match dollar sign "\$". ... been there, done that, works fine.


but here's new problem: dollar signs "\$" next word boundaries marked "\b". ... following examples can reproduced on e.g. regexpal.com.

let's start following text search in:

dollar 50

dollars 50

$ 50

usd 50

my regex should find either "usd", "dollar", or "$". easy enough: let's try

(usd|dollar|\$)

success: finds "$", "usd", , both "dollar" occurrences, including in "dollars".

but let's try skip "dollars" adding word boundaries after multiple choice:

(usd|dollar|\$)\b

and trouble: "usd" matched, "dollar" matched, "dollars" rejected ... single, backslashed (or escaped) "$" rejected well, although worked second before.

it's not related multiple choice inside brackets: try just

\$

vs.

\$\b

and it's same: first 1 matches dollar sign, second 1 doesn't.


another finding:

(usd|dollar|\$) \b

with blank " " between ")" , "\b" works. workaround might not viable under circumstances (in case there should non-whitespace word boundary).


it seems escaped dollar sign refuses found when word boundaries involved.

i'd love hear suggestions solve mystery. -- lot in advance!

it doesn't match, because in $ there isn't word boundary after $. there be, however, if word started after $ - example

$millions

will match.

what want make \b apply cases want match word boundary - example

(usd\b|dollar\b|\$) 

this insist on there being word boundary after "usd" , after "dollar", not after "$".


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -