2

How would this be done in Javascript?

strCellID.Replace(Regex.Match(strCellID, @"(?<=_m)\d+(?=_l)").Value, "-");

1 Answer 1

4

That regular expression uses a look-behind assertion and those aren't supported in JavaScript. You could use sub-expression captures to achieve the same result, though:

// $1 references the first captured sub-expression 
strCellID.replace(/(_m)\d+(?=_l)/g, "$1-");

See also:

Sign up to request clarification or add additional context in comments.

8 Comments

@user517406: There's nothing wrong with the syntax, the regex parses just fine. Doesn't seem to work isn't a very good description of a problem. However, I'd guess that you're probably not assigning the result to anything - strings are immutable so replace() will not change the original string, it will return a new string containing the result instead. If you put a strCellID = in front of the line of code I gave you, it would solve that particular problem. If that's not your problem, you'll need to be more specific. Maybe you could provide some input and expected output samples?
Thanks, that was the problem I had. If I wanted to do something like this codenewCellID = newCellID.replace(/(_l)\d+(?=_o)/g, "$1" + msg.d); code would I be able to use the same syntax?
@user: yeah, there's no reason that wouldn't work (assuming your msg object is set up correctly).
Tried it and the replace doesn't happen i.e. the string remains in it's original state. I have tried both with msg.d and after converting msg.d to a string
@user517406: you'd have to show more code, I'm afraid. If the string returned is the same as the original string it indicates that no matches were found, or the replacement string is exactly the same as the content being replaced. I would check your input string first. msg.d is converted to a string automatically by the + concatenation operator.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.