0

For example, consider Javascript object document.I wish to replace the string "document." with "doc." such that:

  1. In a case such as xyz.document.abc "document." is not replaced.
  2. When a variable is of form predocument.xyz "document." is not replaced.
  3. When there is a case such as document.abc or (document.abc or =document.abc or +document.abc etc "document." is replaced.

Since Javascript does not support look behind regex please suggest an alternative

2
  • I need the cases when the word document be preceded by a "." character or a "\w" character to be excluded Commented Jun 22, 2017 at 16:03
  • 3
    ([^\w.])document(?=[^\w]|\b) and replace with $1doc. Regex101 demo. Commented Jun 22, 2017 at 16:09

1 Answer 1

1

Though originally posted as a comment, I'll allow myself to post my solution once again as an answer, because I don't want this question to hang somewhere as "open". Also, initially I thought that someone would come with a better solution in a second, but apparently it never happened, so there you go:

([^\w.])document(?=[^\w]|\b)

and replace with

$1doc

Regex101 demo.

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

Comments

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.