Hello. Suppose there is such a pattern, a-zA-Zа-яёА-ЯЁ0-9\-\_ . I use it in match() regular expression

Here is the full regular expression:

 'AQwerty_083аСBlah-‌​Blah-Blah'.match(/[a-zA-Zа-яёА-ЯЁ0-9\-\_]/gi); // оно вернёт: ["A", "Q", "w", "e", "r", "t", "y", "_", "0", "8", "3", "а", // "С", "B", "l", "a", "h", "-", "B", "l", "a", "h", "-", "B", "l", "a", "h"] 

There are no problems with this. Further, in a special function, a template is formed, which is returned approximately in the following form: "a-zA-Zа-ЯЁА-ЯЁ0-9-_", as a string. Then I try to use this template in the regular schedule, like this:

 var tmp_rg = tmp_rg('eng+rus+fig+-+_'); // "a-zA-Zа-яёА-ЯЁ0-9\-\_" var regexp = /[tmp_rg]/gi; var matches = "AQwerty_083аСBlah-Blah-Blah".match(regexp); // возвратит: ["r", "t", "_"], т.к. не воспринимает переменную console.log(matches); 

This is clear to me. But then I tried to put a variable through new RegExp(tmp, 'gi') into the regular routine itself. And wrote it like this:

 var tmp_rg = tmp_rg('eng+rus+fig+-+_'); // "a-zA-Zа-яёА-ЯЁ0-9\-\_" var matches = "AQwerty_083аСBlah-Blah-Blah".match(new RegExp(tmp_rg, 'gi')); // возвратит: null, ну а тут я не знаю в чем дело console.log(matches); 

Explain to me people who know why this is happening? Why can't the variable be added to the regular? Already the 2nd week I suffer with these regulars.

  • one
    tmp_rg() returns a string of the type [a-zA-Zа-яёА-ЯЁ0-9\-\_] or immediately RegExp? Something is unclear as 'AQwerty_083аСBlah-Blah-Blah'.match(a-zA-Zа-яёА-ЯЁ0-9\-\_); generally works for you. Must be 'AQwerty_083аСBlah-Blah-Blah'.match(/[a-zA-Zа-яёА-ЯЁ0-9_-]/g); - Wiktor Stribiżew
  • returns: "a-zA-Zа-яёА-ЯЁ0-9 \ -_" together with quotes - J. Doe
  • one
    In general, you should not assign the value of a variable function whose name matches the function name: var tmp_rg = tmp_rg('eng+rus+fig+-+_'); - Grundy
  • one
    @Grundy, if the function is at one time, then does it matter? Although why then do a separate function ... - user207618
  • @ WiktorStribiżew, corrected, was originally written, as you wrote, like this: 'AQwerty_083аСBlah-‌​Blah-Blah'.match(/[a-zA-Zа-яёА-ЯЁ0-9\-\_]/gi) - J. Doe

2 answers 2

Use the constructor to create a RegExp object from a variable:

 var tmp_rg = "a-zA-Zа-яёА-ЯЁ0-9\\-_"; var regexp = RegExp("[" + tmp_rg + "]", "gi"); var matches = "AQwerty_083аСBlah-Blah-Blah".match(regexp); console.log(matches); 

Note that the sign does not have to be escaped at a given place or at the beginning or end of a character class; however, if the template needs to be changed later, it is safer to save the screening.

Character class - [...] - finds 1 character from the character sets and character ranges defined in it.

  • How simple and easy you get. Thank you very much. - J. Doe
  • one
    It is easy, if in tmp_rg all ranges and characters are specified as necessary, then what is needed is escaped (in the code with the help of two backslashes). The underscore symbol _ never necessary to be escaped in any type of regulars. - Wiktor Stribiżew
  • I’m talking about something else, you needed less than 5 minutes to solve this problem, and I’m been tormenting it for several days. Regulars for me is a bad dream, unfortunately. 2nd week do not give rest. - J. Doe
  • one
    If you know English, you can quickly learn the basics of RegexOne . On the MDN website, you can find everything related to the implementation of regulars in JS, and on SO, such problems have long been described. - Wiktor Stribiżew
  • one
    SO = StackOverflow. And here's another link to Wikibooks for regular expressions . It is easy to test expressions on Regex101.com , and for JS there is also Regexr . - Wiktor Stribiżew

With the RegExp designer, everything is fine, but the regular is incorrect.
It is necessary to make a character class - [a-zA-Zа-яёА-ЯЁ0-9\-\_] .

  • Did not help, because the pattern is returned as a string. - J. Doe
  • one
    Well, correct your function to return the character class. - user207618
  • Corrected, it was originally as follows: 'AQwerty_083аСBlah-‌‌​​Blah-Blah'.match(/[a‌​-zA-Zа-яёА-ЯЁ0-9\-_]‌​/gi) - J. Doe