0

Can you please help me. How can I add this regex (?<=^|\s):d(?=$|\s) in javascript RegExp?

e.g

regex = new RegExp("?????" , 'g');

I want to replace the emoticon :d, but only if it is surrounded by spaces (or at an end of the string).

2
  • regex = new RegExp("(?<=^|\s):d(?=$|\s)" , 'g'); This line gives me the error Uncaught SyntaxError: Invalid regular expression: /(?<=^|s):d(?=$|s)/: Invalid group Commented Dec 9, 2012 at 15:59
  • I've tried but the error still exist. And one more thing is that I want to modify the center characters ":d" each time Commented Dec 9, 2012 at 16:03

2 Answers 2

3

Firstly, as Some1.Kill.The.DJ mentioned, I recommend you use the literal syntax to create the regular expression:

var pattern = /yourPatternHere/g;

It's shorter, easier to read and you avoid complications with escape sequences.

The reason why the pattern does not work is that JavaScript does not support lookbehinds ((?<=...). So you have to find a workaround for that. You won't get around including that character in your pattern:

var pattern = /(?:^|\s):d(?!\S)/g;

Since there is no use in capturing anything in your pattern anyway (because :d is fixed) you are probably only interested in the position of the match. That means, when you find a match, you will have to check whether the first character is a space character (or is not :). If that is the case you have to increment the position by 1. If you know that your input string can never start with a space, you can simply increment any found position if it is not 0.

Note that I simplified your lookahead a bit. That is actually the beauty of lookarounds that you do not have to distinguish between end-of-string and a certain character type. Just use the negative lookahead, and assure that there is no non-space character ahead.

Just for future reference that means you could have simplified your initial pattern to:

(?<!\S):d(?!\S)

(If you were using a regex engine that supports lookbehinds.)

EDIT:

After your comment on the other answer, it's actually a lot easier to use the workaround. Just write back the captured space-character:

string = string.replace(/(^|\s):d(?!\S)/g, "$1emoticonCode");

Where $1 refers to what was matched with (^|\s). I.e. if the match was at the beginning of the string $1 will be empty, and if there was a space before :d, then $1 will contian that space character.

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

Comments

1

Javascript doesnt support lookbehind i.e(?<=)..

It supports lookahead

Better use

/(?:^|\s)(:d)(?=$|\s)/g

Group1 captures required match

5 Comments

JavaScript does not support lookbehinds
@m.buettner so stupid of me to forget that..edited the ans..thxx
Thanks for your answer. My goal is to replace the emoticon :D with an icon! But i want to replace it only in case of individual words! eg fjkdshk:)jhds. Your modified regex unfortunaly dont work as expected and replace only if the text starts with that symbol
@George that is vital information for how to use the workaround. see the edit of my answer
@m.buettner as always..ur ans is comprehensive and to the point..;)

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.