Is indexOf faster than regex?
Is indexOf faster than regex?
Is indexOf faster than regex?
9 Answers. For just finding a keyword the IndexOf method is faster than using a regular expression. Regular expressions are powerful, but their power lies in flexibility, not raw speed. They don’t beat string methods at simple string operations.
Does indexOf accept regex?
indexOf() that takes a regular expression instead of a string for the first first parameter while still allowing a second parameter ? str. lastIndexOf(/[abc]/ , i); While String.search() takes a regexp as a parameter it does not allow me to specify a second argument!
Is regex fast JavaScript?
Regular expressions seem to be faster (at least in Chrome 10). It cannot be said with certainty which method is faster. The differences between the browsers is enormous. While in Chrome 10 indexOf seems to be faster, in Safari 5, indexOf is clearly slower than any other method.
Which is faster regex or string contains?
To determine which is the fastest you will have to benchmark your own system. However, regular expressions are complex and chances are that String. Contains() will be the fastest and in your case also the simplest solution.
Is regex faster than string compare?
String operations will always be faster than regular expression operations. Unless, of course, you write the string operations in an inefficient way. Regular expressions have to be parsed, and code generated to perform the operation using string operations.
What does regex match return?
The Match(String) method returns the first substring that matches a regular expression pattern in an input string. For information about the language elements used to build a regular expression pattern, see Regular Expression Language – Quick Reference.
How efficient is regex?
Regular expression efficiency can matter. There are some regular expressions that can be astonishingly slow to match with some regular expression implementations. Russ Cox gives an example of a regular expression that takes Perl a minute to match against a string that’s only 29 characters long.
What indexOf () will do?
The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex .
Is it better to use regex or indexOf?
I doubt it – indexOf is a very simple algorithm that will just seek through your string and return the first occurrence it finds. Regex is a far more complex mechanism that needs to be parsed and checked against the whole string. If your string is very large, you are better off with indexOf.
Is there an equivalent of string.indexof ( ) in JavaScript?
In javascript, is there an equivalent of String.indexOf () that takes a regular expression instead of a string for the first first parameter while still allowing a second parameter ? While String.search () takes a regexp as a parameter it does not allow me to specify a second argument!
How long does it take to search a string in regex?
Command one (indexof standard) needs ~ 7500 ms to search the string Command two (indexof with ordinal) needs ~ 300 ms ! command three (regex) needs ~ 650 ms (~1000ms with IgnoreCase option). You can find information about this very query on this link: http://ayende.com/blog/2930/regex-vs-string-indexof
Is there a version of string.indexof ( )?
To start the search from a particular position (faking the second parameter of .indexOf ()) you can slice off the first i characters: But this will get the index in the shorter string (after the first part was sliced off) so you’ll want to then add the length of the chopped off part ( i) to the returned index if it wasn’t -1.