Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with JavaScript's regular expression notation?

I was reading Douglas Crockford's web page, JavaScript: The World's Most Misunderstood Programming Language, and I couldn't help but notice that, under Design Errors, he mentions "the notation for literal regular expressions." What exactly is he talking about? What's wrong with JavaScript's notation for regular expressions, and why?

like image 629
Sasha Chedygov Avatar asked Jun 20 '10 06:06

Sasha Chedygov


People also ask

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1. 1* means any number of ones.

What does .*?) Mean in regex?

(. *?) matches any character ( . ) any number of times ( * ), as few times as possible to make the regex match ( ? ). You'll get a match on any string, but you'll only capture a blank string because of the question mark.

What is invalid regex?

The JavaScript exception "invalid regular expression flag" occurs when the flags in a regular expression contain any flag that is not one of: g , i , m , s , u , y or d .

What does \s mean in regex?

The regular expression \s is a predefined character class. It indicates a single whitespace character. Let's review the set of whitespace characters: [ \t\n\x0B\f\r]


1 Answers

Might have to do with the fact that it enforces you to escape / characters, perhaps he wanted a more unique character to use as the notation.

/test// is invalid, while /test\// is a valid regex.

Whereas in some languages you can actually specify the denotion character in a string, eg:

$regex = '#test/#';

Where # symbols do the denotion.

like image 100
meder omuraliev Avatar answered Sep 23 '22 02:09

meder omuraliev