Javascript regex: match anything as long as something (if it's there)

Hi I am new to regex and this can be a very simple question (hopefully).

I am trying to use one solution for three types of strings

  • "45%", expected result: "45"
  • "45", expected result: "45"
  • ", Expected Result:" "

What I'm trying (let the string be str):

str.match(/(.*)(?!%*)/i)[1] 

This in my head will sound like "match any instance of something to"% "if it is found, or just match something"

In the head of firebug, it seems more like that "it simply matches anything and completely ignores the negative look." Also, to make him lazy - (.*)? - doesn't seem to help.

We remember the second that in this particular situation I only compare numbers, therefore a /\d*/ . I am trying to understand a general rule so that I can apply it every time.

Would anyone be so kind as to help me?

+8
javascript regex negative-lookahead
source share
5 answers

How about a simpler

 str.match(/[^%]*/i)[0] 

This means that the match character is zero or more, which is not % .


Edit: if you need to parse to </a> , then you can analyze the sequence of pf characters and then </a> and then discard </a> , which means that you should use a positive forecast ahead, not a negative one.

 str.match(/.*?(?=<\/a>|$)/i)[0] 

This means: matching the zero-or-more character is lazy, until reaching </a> or the end of the line.

Note that *? is the only operator, (.*)? does not match .*? .

(And don't parse HTML with one regex , as usual.)

+21
source share

I think this is what you are looking for:

 /(?:(?!%).)*/ 

. matches any character, but only after a negative look (?!%) confirms that the character is not % . Note that when the controller is a single character, for example % , you can use a negative character class instead, for example:

 /[^%]*/ 

But for a multi-character watch type such as </a> , you should use the lookahead approach:

 /(?:(?!</a>).)*/i 

It actually says, "Match zero or more characters one at a time, but if the next character is the beginning of the sequence </a> or </a> , stop without consuming it."

+4
source share

The easiest way to use the exact string is to skip the regular expressions and just use indexOf , for example:

 // String to be searched var s = "Here is a <a>link</a>." // String to find var searchString = "</a>"; // Final match var matched = ""; var c = s.indexOf(searchString); if (c >= 0) { // Returns the portion not including the search string; // in this example, "Here is a <a>link". If you want the // search string included, add the length of the search // string to c. matched = s.substring(c); } 
+3
source share

I just wrote it exactly as you said it:

 str.match(/(^[^%]*$)|^([^%]*)%.*/i) 

This will match any line without "%" or the first part of the line containing%. You should get a result from the 1st or 2nd group.

EDIT: This is exactly what you want below

 str.match(/(?:^[^%]*$)|^(?:[^%]*)(?=%)/) 
  • ??: removes all groupings
  • ?? = is a lookahead to see if the string contains%
  • and [^%] matches any character that is not%

so that the regular expression matches any string that does not contain%, OR (otherwise matches) all characters up to the first%

0
source share

to fit 45, 45%, and any amount of any length uses this (182%, 18242, etc.)

 str.match(/([0-9]+)([%]?)/)[1]; 

if you need to match an empty string, include it as ^ $ , note the match ("...") [1] will be undefined for the empty string , so you will need to check the match, and then check [0] or see if whether [1] is undefined.

 str.match(/([0-9]+)([%]?)|^$/) 

if you need to match exactly two digits, use {2,2} to bind the expression between the characters of the beginning and end of the line: " ^ (exp) $ "

 str.match(/^([0-9]{2,2})([%]?)$/)[1]; 
0
source share

All Articles