最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex that matches all characters until 2 white spaces - Stack Overflow

programmeradmin2浏览0评论

I'm using node.js to pick out field values in some converted text. It's pretty messy with random spaces. One line looks like this:

'ADDRESS: 2626 W MAIN ST                                       Tran Total $1.91.$3'

I know where fields start but I can't get it to sop after two white spaces. Here's what I'm trying but it returns everything to the right of 'ADDRESS:':

/ADDRESS:\s([[^\s{2,}]+)/

Is there a way to include spaces and stop at the first double space with a lookahead? This doesn't work:

/ADDRESS:\s(.*(q!\s{2,}))/

I'm using node.js to pick out field values in some converted text. It's pretty messy with random spaces. One line looks like this:

'ADDRESS: 2626 W MAIN ST                                       Tran Total $1.91.$3'

I know where fields start but I can't get it to sop after two white spaces. Here's what I'm trying but it returns everything to the right of 'ADDRESS:':

/ADDRESS:\s([[^\s{2,}]+)/

Is there a way to include spaces and stop at the first double space with a lookahead? This doesn't work:

/ADDRESS:\s(.*(q!\s{2,}))/
Share Improve this question edited Jun 25, 2013 at 0:06 Ry- 225k56 gold badges489 silver badges497 bronze badges asked Jun 25, 2013 at 0:04 jabbermonkeyjabbermonkey 1,8204 gold badges20 silver badges38 bronze badges 1
  • What if there aren't two white spaces, but instead the address ends with end of string or newline? – Paul Commented Jun 25, 2013 at 0:07
Add a comment  | 

3 Answers 3

Reset to default 14

Use a non-greedy wildcard:

/ADDRESS:\s.*?(?=\s{2})/

REGEXR

/ADDRESS:\s((?:\S|\s(?!\s))*)/

should do the trick. Any number of non-spaces or spaces not followed by a space.

Why not just do this?

out = input.split("  ")[0]; // two spaces in that string

That's more effective than a regex. Or try this:

var spaces = input.indexOf("  "); // again, two spaces
out = spaces < 0 ? input : input.substr(0,spaces);
发布评论

评论列表(0)

  1. 暂无评论