Steven
Steven

Reputation: 1082

Javascript regex match escaped html character-code

My javascript regex is not working properly.

I want to detect A-Z, a-z, 0-9, WHITESPACE and ' _ - .

The problem is: The apostroph is escaped. So its '

So DJ Blubeispiel - I'm Walking In The Darkness.mp3

becomes DJ Blubeispiel - I'm Walking In The Darkness.mp3

How can I detect the escaped apostroph?


Here is my regex:

var regex = /^[A-Za-z0-9\s\(\)\.'_-]{1,}$/;

The regex is looking for ' and not for '

Upvotes: 2

Views: 2099

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149030

Try this:

regex = /^([\w\s.-]|')+$/

The \w represents any 'word' character, which includes both upper- and lower-case letters, digits, and underscores. The | is an alternation, which will first attempt to match the pattern to the right (within the surrounding group), and if it fails, then attempt to match the pattern to the right. The + is just a shortened form of {1,}.

So this matches any string which consists of one or more instances of word characters, whitespace characters, periods, or hyphens, or the sequence '.

Upvotes: 3

Related Questions