-1

I have this Javascript I found and have to use but I don't understand what it is doing. Specifically, the regexp_quote() function. Can anyone help me understand what it would be trying to do here? It appears to be doing nothing but there might be some scenario where it actually does something.

function replace(input, pattern, flags, str){

    return input.replace(new RegExp(regexp_quote(pattern), flags), str);
}
function regexp_quote(str) {
     return str.replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
 }

var dateTime = "2016-03-09T13:18:21-05:00"
dateTime.replace(new RegExp(regexp_quote(':'), 'g'), '.')
Brian T Hannan
  • 3,687
  • 15
  • 49
  • 88
  • It adds escape markers to common RegExp characters. Perhaps you should read this article on [Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions). Just try `regexp_quote('.?*+^$[\]\\(){}|-')`, and it's _very_ obvious what it is doing. – Oka Mar 09 '16 at 18:33
  • Aha! I see it now! Thank you .... that sneaky \\$1 got me. – Brian T Hannan Mar 09 '16 at 18:35
  • @Oka if you make an answer I'll accept it – Brian T Hannan Mar 09 '16 at 18:35
  • Possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Mathletics Mar 09 '16 at 18:43
  • @BrianTHannan Sure thing. – Oka Mar 09 '16 at 18:47

1 Answers1

1

/([.?*+^$[\]\\(){}|-])/ is a Regular Expression with a single capture group, encompassing a character set, which in turn contains common characters that have special meaning in Regular Expressions.

The function regexp_quote takes a String, and globally replaces each instance of a character found in the set, with an escaped version (one with a preceding backslash). $1 indicates use of the first (and only) capture group.

regexp_quote('.?*+^$[\]\\(){}|-') returns '\.\?\*\+\^\$\[\]\\\(\)\{\}\|\-', showing the very obvious escape prefixing of characters.

Oka
  • 14,862
  • 5
  • 34
  • 46
  • "Common characters", or "all characters"? –  Mar 09 '16 at 18:57
  • 1
    @torazaburo Common. There are other characters that have special meaning in Regular Expressions, when escaped (`d`, `w`, `s`, etc.). – Oka Mar 09 '16 at 18:59
  • Well, those aren't really "special characters" and don't need to be handled separately, since escaping the backslash will take care of them. –  Mar 12 '16 at 10:36
  • You're muddling words by trying to be pedantic like this. I did not say that `d`, `w`, `s` are special characters in regular expressions, but their escaped versions have _special meaning_. The current set of characters _lose_ special meaning when escaped. If you added `bdstw` to the start of that character set, you would be in effect _inversely adding_ functionality to a `RegExp` constructed from the resulting string, but there's no need to get so bloody precise in an answer of this magnitude - one that barely scratches the surface of regular expressions. – Oka Mar 12 '16 at 17:38