Question

I try to check for a given RegExp-rule in a string and need to get the current matching rule.

Here's what I've tried so far:

var prefixes = /-webkit-|-khtml-|-moz-|-ms-|-o-/g;
var match;
var str = '';

while ( !(match = prefixes.exec(str)) ) {
    str += '-webkit-';
    console.log(match); // => null
}

The match is null, but how can I get the current matching-rule (in this case -webkit-)?

Was it helpful?

Solution 2

You aren't asking for any groups in your regex, try surrounding your regex in parenthesis to define a group, e.g. /(-webkit-|-khtml-|-moz-|-ms-|-o-)/g.

Various other issues, try:

var prefixes = /(-webkit-|-khtml-|-moz-|-ms-|-o-)/g;
var match;
var str = 'prefix-ms-something';

match = prefixes.exec(str);
console.log(match);

OTHER TIPS

var prefixes = /(-webkit-|-khtml-|-moz-|-ms-|-o-)/g;
var str = "-webkit-adsf-moz-adsf"
var m;
while(m = prefixes.exec(str))
    console.log(m[0]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top