开发者

I need help trying to write a javascript regular expression

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 开发者_JS百科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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜