6

I am trying to take the value of an input text field and use it within a regular expression. Here's what I have to match the start of a line.

regex = new RegExp('^' + inputValue, 'i')

It works fine for regular strings that start have alphanumeric characters, but I am using this for dollar amounts as well. When the input field starts with a '$' (dollar sign) i get varying results.

How can I escape the variable above to be viewed as a simple string? I've tried this, but no luck:

regex = new RegExp('^[' + inputValue + ']', 'i')
CoryDorning
  • 1,854
  • 4
  • 25
  • 36
  • PHP has `preg_quote`, I don't think JavaScript has one. The PHP function escapes `. \ + * ? [ ^ ] $ ( ) { } = ! < > | : -`, so I guess just `replace` these with their escaped versions. – gen_Eric Mar 08 '12 at 17:15
  • 2
    Look at the answer in this question http://stackoverflow.com/questions/3614440/replicate-the-functionality-of-javas-pattern-quote-in-a-javascript-regexp – Danny Mar 08 '12 at 17:15
  • 1
    [This](http://simonwillison.net/2006/Jan/20/escape/#p-6) perhaps. – pimvdb Mar 08 '12 at 17:15
  • Duplicate of http://stackoverflow.com/a/6969486/151312 function escapeRegExp(str) { return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); } – coolaj86 Jun 15 '12 at 15:42
  • Does this answer your question? [Escape string for use in Javascript regex](https://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex) – T.Todua Jun 27 '20 at 22:07

3 Answers3

4

Probably one of the better implementations I've seen

http://phpjs.org/functions/preg_quote:491

Copied below for posterity

function preg_quote (str, delimiter) {
    // Quote regular expression characters plus an optional character  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/preg_quote    // +   original by: booeyOH
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Brett Zamir (http://brett-zamir.me)    // *     example 1: preg_quote("$40");
    // *     returns 1: '\$40'
    // *     example 2: preg_quote("*RRRING* Hello?");
    // *     returns 2: '\*RRRING\* Hello\?'
    // *     example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");    // *     returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:'
    return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&');
}
Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
1

$ matches the end of a line, so if you inputValue starts with $, you're looking for matches like ^$9.99, which obviously wont match anything because 9.99 can't come AFTER the end of a line.

if $ is the only character you're worried about, you can just run inputValue through a find/replace so that $ is escaped to \$.

Alexander Corwin
  • 1,097
  • 6
  • 11
0

Did not you think about just checking if string starts with your input with indexOf ?

Do it in following way:

var inputValue = "some";
if ( "some string".indexOf(inputValue.toLowerCase()) == 0 ) {
  // test passed
}
hsz
  • 148,279
  • 62
  • 259
  • 315