Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why slice() and substr() methods in javascript doesn't work on regular expressions?

my code is:

var st="man myfile=l/p/nm.mp3 yourfile=/o/mj/nnc.mp3 ou p";
var patt=/myfile.[\W|\w]*.mp3\s/;
var s=patt.exec(st);
var s2=s.slice(3,4);
alert(s2);

but slice() gives me nothing and substr() method gives me an error:

Object doesn't support this method

Why?

like image 493
Saeed Noori Avatar asked Jan 18 '26 17:01

Saeed Noori


1 Answers

Fabricio is right. Your variable s is a RegExp object. To access the substring you'll need to do:

var s2 = s[0].substr(3,4);

jsFiddle here.

like image 177
hohner Avatar answered Jan 20 '26 08:01

hohner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!