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.
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żewvar tmp_rg = tmp_rg('eng+rus+fig+-+_');
- Grundy'AQwerty_083аСBlah-Blah-Blah'.match(/[a-zA-Zа-яёА-ЯЁ0-9\-\_]/gi)
- J. Doe