1

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.

  • Try `cExp = /oErro(?:\.\w+)?/g` or `cExp=/oErro(?:\.[^\s;,)}]+)?/g` and then you would need to use `.replace(cExp, '\' + $& + \'')` – Wiktor Stribiżew Apr 19 '20 at 17:41
  • @WiktorStribiżew Works like a charm! How can I mark this comment as an answer? To be quite honest, I don't understand how nor why: seems to me that the question mark turns optional the dot and the word (inside parenthesis) right after the search term, but I'll search for the meaning of colon because that is not clear for me... Thank you very much! – Sidnei Paixao Apr 19 '20 at 18:09
  • See my answer below, I added some details and helpful links. Glad it worked for you. – Wiktor Stribiżew Apr 19 '20 at 18:13

2 Answers2

1

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 }.
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • There is any difference in JavaScript between this `cExp=/oErro(?:\.\w+)?/g` and this `cErro = "oErro"; cExp = new RegExp(cErro + "(?:\.\w+)?", "g")`? The result is quite different when using one or another.... while first gives me `Error ocurred: ' + oErro.message + ' (complete message: ' + oErro + ')`, the second strangely gives me `Error ocurred: ' + oErro + '.message (complete message: ' + oErro + ')`... – Sidnei Paixao Apr 19 '20 at 19:24
  • 1
    @SidneiPaixao To introduce a backslash in a simple string literal you need to use `"\\"`. So, you need `cExp = new RegExp(cErro + "(?:\\.\\w+)?", "g")`. If you use the latest ECMAScript standard, use raw string literals, ``cExp = new RegExp(String.raw`${cErro}(?:\.\w+)?`, "g")`` – Wiktor Stribiżew Apr 19 '20 at 19:26
  • Completely forgot the double backslashes! Sorry for that... Adjusted my code and now it's working fine... maybe it's time for sleep, or a cup of coffee... – Sidnei Paixao Apr 19 '20 at 19:31
1

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 + ')"