Forum Moderators: open

Message Too Old, No Replies

expr.test(str), using /x modifier and compatability

         

csdude55

7:42 pm on Mar 26, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have a function that has about 30 indexOf statements, and I think it would be faster and easier to read if I used .test() instead:

[koukia.ca...]

According to this, I can use the /x modifier:

[tutorialrepublic.com...]

so my code can look like:

var expr = /
csdude | // my name
foo | // random
bar // another random
/gix;

That cuts my code in less than half! And according to the benchmark test, it should be faster to process.

JSFiddle throws an error when I do this, though, so I'm not sure if this is going to be OK with all browsers? I can't find the compatability of /x on Google, and caniuse.com doesn't tell me unless I should search for it a different way:

[developer.mozilla.org...]
[caniuse.com...]

csdude55

2:42 am on Mar 27, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, for future reader... I'm pretty sure that tutorialrepublic.com is wrong, the /x modifier isn't recognized by Javascript :-( It would have been helpful for the sake of readability, but oh well.

As far as I can tell, though, expr.test(str) is still faster than indexOf.