7

In JavaScript, how do you use use the backtick (`) in a regular expression?

Sample code:

xtype: 'textfield',
regex: /^[a-zA-Z0-9àâçéèêîïôùû-.,:+*'()=&_ \s\u0060]+$/
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Java Learner
  • 311
  • 2
  • 10
  • 26

3 Answers3

16

backtick has no special meaning. you can use it as /`/, it will work fine.

Mr_Green
  • 40,727
  • 45
  • 159
  • 271
schnill
  • 905
  • 1
  • 5
  • 12
  • What about the new [template literals](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals) – olfek Apr 28 '17 at 15:48
  • @daka Doesn't change anything. Still no special meaning in a regular expression. – Trott Jun 20 '18 at 22:04
  • If you want to use template literal backticks in a regular expression see https://stackoverflow.com/questions/43390873/template-literal-inside-of-the-regex. In short use new RegExp('regexp') instead of /regexp/ – AndrewHarvey Dec 27 '19 at 04:13
4

Use unicode character searches and it should do it. Unicode character code for backtick is \u0060

So /\u0060/ should find you backticks. Tested it on RegexPal and it works.

However, as previous respondent correctly said, ` should just work fine without escaping. You must have the problem somewhere else if it doesn't. But using the unicode will ensure that it will definitely match.

mavili
  • 3,385
  • 4
  • 30
  • 46
  • and Srb1313711, this is my regex /^[a-zA-Z0-9àâçéèêîïôùû-.,:+*'()=&_ \s]+$/ i have a text box where i can create a record here its not accepting backtick, i am not allowed to enter backtick, it shows red mark. – Java Learner Jul 24 '13 at 09:42
  • 1
    @RajasekharP I just test your regex with [JS Regex Tester](http://www.regular-expressions.info/javascriptexample.html) and it's fine it works. if you can't enter backticks to the textbox, then the unicode character should do the job. – mavili Jul 24 '13 at 09:51
  • can you please edit my regex dat fits with unicode character. – Java Learner Jul 24 '13 at 09:55
  • /^[a-zA-Z0-9àâçéèêîïôùû-.,:+*'()=&_ \s\u0060]+$/ – mavili Jul 24 '13 at 09:56
  • just make sure that the textbox you're entering your regex in accepts regex in that format. some of them don't expect you to include the delimeters (//), just the regex plain. – mavili Jul 24 '13 at 09:57
  • so in that case, you'll enter `^[a-zA-Z0-9àâçéèêîïôùû-.,:+*'()=&_ \s\u0060]+$` ONLY, without opening and closing delimeters – mavili Jul 24 '13 at 09:57
  • still it does nor works :(, to be more clear, its EXTJS xtype: 'textfield', regex: /^[a-zA-Z0-9àâçéèêîïôùû-.,:+*'()=&_ \s\u0060]+$/ i have tried both ways with and with out delimeters (//). – Java Learner Jul 24 '13 at 10:05
  • @RajasekharP I know nothing about ExtJS but have you tried enclosing your regex in quotes? so xtype: 'textfield', regex:'' – mavili Jul 24 '13 at 10:08
  • sorry, can't be of any more help :( – mavili Jul 24 '13 at 10:17
1

I dont actually think that this character needs escaping, look here for the characters that need escaping

You can also try here to test which I find very helpful.

Srb1313711
  • 2,017
  • 5
  • 24
  • 35