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')
5
  • 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. Commented Mar 8, 2012 at 17:15
  • 2
    Look at the answer in this question stackoverflow.com/questions/3614440/… Commented Mar 8, 2012 at 17:15
  • 1
    This perhaps. Commented Mar 8, 2012 at 17:15
  • Duplicate of stackoverflow.com/a/6969486/151312 function escapeRegExp(str) { return str.replace(/[\-[]\/\{\}()*\+\?\.\\\^\$\|]/g, "\\$&"); } Commented Jun 15, 2012 at 15:42
  • Does this answer your question? Escape string for use in Javascript regex Commented Jun 27, 2020 at 22:07

3 Answers 3

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'), '\\$&');
}
Sign up to request clarification or add additional context in comments.

Comments

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 \$.

Comments

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
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.