[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

Regex - test for a percentage but only in the first line

Consider a string like the following

blah 10% blah
up to 20% test
blah blah

I'm looking for a regex that would return the 10% and not the 20%

And a string like

blah blah
up to 20% test
blah blah

would return nothing
I'm using javascript
Thanks
0
joegass
Asked:
joegass
  • 3
  • 3
1 Solution
 
lbertaccoCommented:
This should work:
myString.match(/^[^\n]*\s\d+%/s)
0
 
joegassAuthor Commented:
I presume you meant to have \s at the end?
Works great thank you
0
 
lbertaccoCommented:
The final "s" was the "single line" flag, but actually that was not necessary.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
joegassAuthor Commented:
Does that flag exist in the javascript implementation?
0
 
lbertaccoCommented:
If I remember right, both the s (single line) and m (multiline) flags should be supported by latest standard (these flags alter the beheaviour only of the . (dot) , ^ and $ patterns). However I didn't check and I don't even remember which one is the default, but if the regex I proposed above works all right on the samples of your question, it means that the default is good enough (that is the first ^ in my regex matches only the beginning of the first line and not the beginning of any line).
0
 
joegassAuthor Commented:
Yes it works fine without it - thanks for your help
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now