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
3 Answers
Reset to default 14Use 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);