Alternation

Alternation is denoted by the vertical line '|'. It allows to choose between multiple variants.

The regexp alternation is like good old “OR” in an if statement.

Programming languages example

The example below uses the alternation to find programming languages:

var re = /html|php|css|java(script)?/gi

var str = "First HTML, then CSS, then JavaScript"

alert( str.match(re) )  // 'HTML', 'CSS', 'JavaScript'

Alternation has very low priority. To match both gray and grey, one should use gr(a|e)y or gr[ae]y, but not gra|ey. The latter applies alternation to subexpressions “gra” (OR) “ey”.

The pattern html|php|css|java(script)?|C finds programming languages.

But it gives false matches, see below:

var re = /html|php|css|java(script)?|C/gi

var str = "Javallo, PHP specialist"

alert( str.match(re) )  // 'Java', 'PHP', 'c'

…But obviously, there is only PHP language in the text. Fix the regexp to make it match correctly.

Open solution
Solution

To filter, we need to match languages only if they are standalone words. A word boundary check helps here: \b(html|php|css|java(script)?)\b.

var re = /\b(html|php|css|java(script)?|C)\b/gi

var str = "Javallo, PHP specialist"

alert( str.match(re) )  // 'Java', 'PHP', 'c'

Create a regexp to validate a nick. A nick consists of a clan and a name:

"[Clan]John", "{CLAN}John", "(Clan)John"

A clan is either in square or curly or round brackets, brackets must be same on both sides. The clan and the name have no spaces:

"[Clan)john" // invalid, different brackets
"[My Clan]john galt"  // clan is invalid, name is invalid (spaces)

The call str.match(your regexp) must return null for invalid nicks and an array for valid ones.

P.S. Can the array contain the clan Clan and the name John?

Open solution
Solution

First, we need to match a clan name. Unfortunately, there is no way to tell the regexp engine that ”if the opening bracket is '[', then the closing should be ']'.

So we have to match all three types of clan names. A clan name is something non-spacy \S+, so there are 3 alternatives for each type of brackets: \[\S+] | \{\S+} | \(\S+\)

Alternatives should be grouped together: (..|..|..). It is required, because we add the next item after them: a name \S+.
It gives us: (\[\S+] | \{\S+} | \(\S+\))\S+

Finally, the pattern should test the whole string, so it starts with '^' and ends with '$': ^(\[\S+] | \{\S+} | \(\S+\))\S+$

(By the way, as you can see most special characters don’t need to be escaped)

In action:

var str = "[Clan]John"

var re = /^(\[\S+]|\{\S+}|\(\S+\))\S+$/
alert( str.match(re) ) // can also re.test

P.S. Now let’s see how to capture the clan. The regexp above actually does that, but the clan alternatives are in a group. But it comes in brackets, like {Clan}.

Of course we’d like to have it without brackets. So, let’s wrap each clan \S+ into brackets (\S+): ^(\[(\S+)] | \{(\S+)} | \((\S+)\))\S+$

The inconvenience is that depending on which bracket matches, the corresponding array item is set. The external code should take care of it.

For example:

var re = /^(\[(\S+)]|\{(\S+)}|\((\S+)\))\S+$/

"[Clan]John".match(re) 
// ["[Clan]John", "[Clan]", *!*"Clan"*/!*, undefined, undefined]

"{Clan}John".match(re) 
// ["[Clan]John", "{Clan}", undefined, *!*"Clan"*/!*, undefined]

"(Clan)John".match(re) 
// ["(Clan)John", "(Clan)", undefined, undefined, *!*"Clan"*/!*]

Write a regular expression to match a double-quoted string.

The string may have escaped quotes \" inside.

For example:

str = ' .. "test" .. '   // matches "test"

// Slashes are escaped 
str = ' .. "say \\"Hello\\"!" .. ' //  "say \"Hello\"!"

str = ' .. "\\" .. '  // no match

Open solution
Solution

The solution is /"(\\.|[^"\\])*"/.

A backslashed symbol \\. (quote or anything) is consumed before non-backslashed match attempt [^"\\].

var re = /"(\\.|[^"\\])*"/g

alert( ' .. "test" .. '.match(re) ) 

alert( ' .. "say \\"Hello\\"!" .. '.match(re) )

alert( ' .. "\\" .. '.match(re) )

Tutorial

Donate

Donate to this project