user2039981
user2039981

Reputation:

Javascript - RegExp - Replace character not surrounded by letters

I have a regex like this:

str.replace(/([^a-zA-Z])e([^a-zA-Z])/g, "$12.718281828459045$2")

The regex isn't doing what I want, and I don't know why.

So what I want to to do, is to replace all "e"'s in a string that's not connected to any A-z letter with "2.718281828459045", what's wrong in my regex?

"2e" -> "2e", should be: "22.718281828459045"
"2e2" -> "22.7182818284590452", working
"2*e*e*2" -> "2*2.718281828459045*e*2", should be "2*2.718281828459045*2.718281828459045*2"

Upvotes: 0

Views: 310

Answers (5)

Kramer
Kramer

Reputation: 13

try with /([^a-zA-Z])?e(?![a-zA-Z])/g And '$12.718281828459045' for replacement.

Using negative look ahead, and an optional previous alpha character as there is no lookbehind in JS (that I know of).

Upvotes: 0

Alxandr
Alxandr

Reputation: 12423

The problem here is that the first one does not match because there is nothing after the e, thus it has to check for an e followed by something that isn't a letter or end of input. However, the third example doesn't work because in 2*e*e*2 the first match is *e*, thus both * are "consumed", so what's left of the string is only e*2. This obviously doesn't fix the problem.

What can be used in stead is a negative lookahead. They are written as a(?!b) in regex and (in this case) means an a not followed by a b. So we make the regex into ([^a-zA-Z])e(?![a-zA-Z). However, this still does not match the simple string e, because there is nothing in front of it. In something that's not JavaScript we could use a negative lookbehind, though js doesn't support this, so rather just change it to (^|[^a-zA-Z])e(?![a-zA-Z]).

The meaning is: Find any e that is at the beginning of the string, or after something that's not a-z or A-Z, and is not followed by an a-z or A-Z.

Here is working demo: http://regex101.com/r/wQ1oW3/3 (note, I replaced with <input> though, because it's simpler to see that the replacing is right.)

Upvotes: 1

Toto
Toto

Reputation: 91385

How about:

find:    /(^|[^a-zA-Z])e(?=[^a-zA-Z]|$)/g
replace: $12.718281828459045

.

str.replace(/(^|[^a-zA-Z])e(?=[^a-zA-Z]|$)/g, "$12.718281828459045")

Upvotes: 0

Kamehameha
Kamehameha

Reputation: 5473

Try this regex -

Regex   - ([^a-zA-Z])?(?:e(?:.*e)?)([^a-zA-Z])?
Replace - $12.718281828459045$2

DEMO

Upvotes: 0

Explosion Pills
Explosion Pills

Reputation: 191729

The second set of parentheses consumes that part of the string for the regex. What that means is that the remaining string e*2 does not match because that e is not preceded by the non-alphanumeric character. You can use a lookahead instead which does not consume.

"2*e*e*2".replace(/([^a-zA-Z])e(?=[^a-zA-Z])/g, "$12.718281828459045")

Upvotes: 0

Related Questions