0

Folks,

I'm trying to replace a huge chunk of string with a multiple occurrences of "${country_id}". I need a Regular expression that can replace the ${country_id}. Here is the code I have:

    var iterLiteral  = "\$\{"+literal+"\}"
    var re = new RegExp(iterLiteral,"g")
    var value = value;
    return body.replace(re,value)

I get this error:

Evaluator: org.mozilla.javascript.EcmaError: Invalid quantifier }

How can I fix it?

Edit:

String to be replaced: ${country_id} literal being passed to the function : country_id.

Trying to use what Anubhava said ( using \\ ), the program tries to search for \$\{country_id\} and it doesn't find one.

Edit 2: Why is this a duplicate? the question that was mentioned doesn't talk about escaping.

abhididdigi
  • 361
  • 4
  • 16

2 Answers2

2

If you have a set regular expression, you might find it easier to use the // syntax for defining the RegExp:

'foo: ${country_id}, bar: ${country_id}'.replace(/\$\{country_id\}/g, 'baz')

Alternatively, if the string must be constructed, then you need to double escape the slashes for them to be a part of the regular expression, and not seen as escaping characters for the creation of the string itself:

'foo: ${country_id}, bar: ${country_id}'.replace(new RegExp('\\$\\{' + 'country_id' + '\\}', 'g'), 'baz')

Your function would thus be:

function replaceLiteral(body, literal, value) {
    var iterLiteral = "\\$\\{" + literal + "\\}";
    var re = new RegExp(iterLiteral, "g");
    return body.replace(re, value)
}

var result = replaceLiteral('foo: ${country_id}, bar: ${country_id}', 'country_id', 'baz');
console.log(result);

All of these output the same string:

'foo: baz, bar: baz'
Dawson Toth
  • 5,580
  • 2
  • 22
  • 37
  • Dawson, You will have to understand that country_id cannot be hardcoded. It's coming as a parameter into the function. The function is iterated over 100 unique values. So I cannot be hardcoding it and so cant use the first method. – abhididdigi Nov 12 '15 at 21:58
  • I am aware. Hence the second and third examples. For those that may find this answer, they may find it easier to construct the RegExp with the first syntax. If, however, they need the flexibility, the second and third will work (as does your use case). – Dawson Toth Nov 12 '15 at 22:00
0

Using double slashes ought to work, if your .replace function is correct (meaning that you use the regex as the first argument, not the iterLiteral). If it doesn't, there's something going wrong else where in your code. If that's the case, please provide the whole function you are using.

function fandr(literal, value, el) {
  var iterLiteral = "\\$\\{" + literal + "\\}",
    re = new RegExp(iterLiteral, "g"),
    $el = $(el);
  console.log(re);
  $el.html(function() {
    return $el.html().replace(re, value);
  });
}

fandr("country_id", "banana", "span");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span>${country_id}</span>
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239