I need help trying to write a JavaScript regular expression to select Harry
and Anderson
from this text:
H6 Harry - Anderson
So I want the word after the hyphen and word before the hyphen.
I need help trying to write a JavaScript regular expression to select Harry
and Anderson
from this text:
H6 Harry - Anderson
So I want the word after the hyphen and word before the hyphen.
Well, if there's always a hyphen present, you can use:
/(\w+) - (\w+)/
(PCRE syntax)
In JavaScript you can use the object method to catch the text in question:
string.match(/(\w+) - (\w+)/);
You can use the /g
modifier to scan the whole chunk of text, it keeps backreferences in $1
up to $99
.