简体   繁体   中英

RegExp to replace exact OR similar word JS

I'm struggling with an Regex to replace the name of a variable inside a string...

I need to put certain variable name outside quotes, and, if that variable have a qualifier (like a property or method), this qualifier need to be outside quotes in the final string, too.

So, given this example:

cExp = new RegExp('oErro', 'g');
cMsg = "Error ocurred: oErro; please try again";
cMsg.replace(cExp, '\' + oErro + \'')

the output is exactly what I expect:

'Error ocurred: ' + oErro + '; please try again'

I search how to include any words after the variable name, and ended up with this piece of code:

cExp = new RegExp('oErro(\.[^\ |^\;|^\,|^\)|^\}]*)', 'g');
cMsg = "Error ocurred: oErro.message; please try again";
cMsg.replace(cExp, '\' + oErro$1 + \'')

and the result is exactly what I expected to see:

'Error ocurred: ' + oErro.message + '; please try again'

So far, so good. But, if I mix variable name with variable.qualifier, things start to get messy:

cExp = new RegExp('oErro(\.[^\ |^\;|^\,|^\)|^\}]*)', 'g');
cMsg = "Error ocurred: oErro.message (complete message: oErro)";
cMsg.replace(cExp, '\' + oErro$1 + \'')

I GET this output

'Error ocurred: ' + oErro.message + ' (complete message: ' + oErro) + ''

while I EXPECTED this output (note the parenthesis INSIDE the quotes)

'Error ocurred: ' + oErro.message + ' (complete message: ' + oErro + ')'

In other words, every time "oErro" is used without a qualifier, the expression gets the next word and join with oErro, outside the enclosing quotes.

Certainly I'm doing something wrong, but I'm not very familiar with RegExp and maybe not searching with correct terms to get appropriate help.

What I need is an expression that works for both scenarios (removing the word "oErro" or the syntax "oErro.something" from quotes in the final string)...

Thanks in advance and sorry for the poor english, I try to put some examples but feel free to ask if you need more details on what I need to achieve.

You may use

cExp=/oErro(?:\.[^\s;,)}]+)?/g
// Or, if the chars after `.` can only only be letters/digits/underscore
cExp = /oErro(?:\.\w+)?/g

Then, you would need to use

cMsg.replace(cExp, '\' + $& +  \'')

where $& is the backreference to the whole match value.

Pattern details

  • oErro - a literal string
  • (?:\.\w+)? - an optional (due to ? at the end) non-capturing group that matches 1 or 0 occurrences of
    • \. - a dot - \w+ - 1+ letters/digits/underscores
    • [^\s;,)}]+ - 1 or more chars other than whitespace, ; , , , ) and } .

I believe your requirement for capturing the property or method name is satisfied by using the \w character in your regex.

With oErro

cMsg = "Error ocurred: oErro; please try again";
cMsg.replace(/(oErro(\.\w+)?)/g, '\' + $1 + \'');
// Output: "Error ocurred: ' + oErro + '; please try again"

With oError.message

cMsg = "Error ocurred: oErro.message (complete message: oErro)";
cMsg.replace(/(oErro(\.\w+)?)/g, '\' + $1 + \'');
// Output: "Error ocurred: ' + oErro.message + ' (complete message: ' + oErro + ')"

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