I’ve got a set of regex’s within a function that are working rather well for me, but I’ve encountered a new pattern where they fail. This function fails when there are no more characters in the string. For example my function matches and replaces text in the following:
“1 m is equivalent to...” becomes “1 meter is equivalent to...” However, it fails on:
“There are 100 cm in 1 m“
I’m using AS3, which I believe has a regex engine nearly equivalent to JavaScript’s. The current pattern is:
[0-9]+ m(?= )|[0-9]+m(?= )
I loop through a list of patterns and replacement strings, so it was easy to add another pattern to the list. I tried:
[0-9]+ m(?=)|[0-9]+m(?=)
And:
[0-9]+ m(?='')|[0-9]+m(?='')
And both failed. I’m missing a fundamental tidbit of knowledge. I believe I need to know how to say, “look-ahead and match when there are no other characters in the line”
You could simplify your expression by looking for a word boundary (
\b); something like this: