简体   繁体   中英

How to write a regex to match a Javascript object name?

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM