JavaScript-Use variable in string match

Znalazłem kilka podobnych pytań, ale mi to nie pomogło. Mam taki problem:

var xxx = "victoria";
var yyy = "i";
alert(xxx.match(yyy/g).length);

Nie wiem jak podać zmienną w Komendzie match. Proszę o pomoc. Dziękuję.

Author: Munawir, 2010-07-03

6 answers

Chociaż funkcja match nie akceptuje liter łańcuchów jako wzorców regex, możesz użyć konstruktora obiektu RegExp i przekazać go do łańcucha.funkcja dopasowania:

var re = new RegExp(yyy, 'g');
xxx.match(re);

Wszelkie potrzebne flagi (np. /g)mogą zostać umieszczone w drugim parametrze.

 195
Author: Chris Hutchinson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-03 21:58:19

Musisz użyć obiektu RegExp Jeśli twój wzorzec to string

var xxx = "victoria";
var yyy = "i";
var rgxp = new RegExp(yyy, "g");
alert(xxx.match(rgxp).length);

Jeśli wzorzec nie jest ciągiem dynamicznym:

var xxx = "victoria";
var yyy = /i/g;
alert(xxx.match(yyy).length);
 10
Author: Anpher,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-03 22:03:55

Na przykład:

let myString = "Hello World"
let myMatch = myString.match(/H.*/)
console.log(myMatch)

Lub

let myString = "Hello World"
let myVariable = "H"
let myReg = new RegExp(myVariable + ".*")
let myMatch = myString.match(myReg)
console.log(myMatch)
 9
Author: Driton Haxhiu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-13 10:07:58

Przykład. aby znaleźć liczbę samogłosek w ciągu

var word='Web Development Tutorial';
var vowels='[aeiou]'; 
var re = new RegExp(vowels, 'gi');
var arr = word.match(re);
document.write(arr.length);
 6
Author: Sarvar Nishonboev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-08 12:07:16

Dla mnie tak czy inaczej, to pomaga zobaczyć go używanego. po prostu zrobiłem to za pomocą przykładu "re":

var analyte_data = 'sample-'+sample_id;
var storage_keys = $.jStorage.index();
var re = new RegExp( analyte_data,'g');  
for(i=0;i<storage_keys.length;i++) { 
    if(storage_keys[i].match(re)) {
        console.log(storage_keys[i]);
        var partnum = storage_keys[i].split('-')[2];
    }
}
 0
Author: geekbuntu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-16 16:31:14
xxx.match(yyy, 'g').length
 -4
Author: SilentGhost,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-03 21:56:30