Hi I am new to regular expression and this may be a very easy question (hopefully).
I am trying to use one solution for 3 kind of string
The easiest way with an exact search string is to skip regular expressions and just use indexOf
, e.g.:
// 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);
}
to match 45, 45%, and any number of any length use this (182%, 18242, etc)
str.match(/([0-9]+)([%]?)/)[1];
if you need to match the empty string also include it as ^$, note match("...")[1] will be undefined for the empty string, so you will need to test for match and then check [0] or see if [1] is undefined.
str.match(/([0-9]+)([%]?)|^$/)
if you need to match exactly two digits use {2,2} anchor the expression between begin and end line characters: "^(exp)$"
str.match(/^([0-9]{2,2})([%]?)$/)[1];
I think this is what you're looking for:
/(?:(?!%).)*/
The .
matches any character, but only after the negative lookahead, (?!%)
, confirms that the character is not %
. Note that when the sentinel is a single character like %
, you can use a negated character class instead, for example:
/[^%]*/
But for a multi-character sentinel like </a>
, you have to use the lookahead approach:
/(?:(?!</a>).)*/i
This is actually saying "Match zero or more characters one at a time, but if the next character turns out to be the beginning of the sequence </a>
or </A>
, stop without consuming it".
I just wrote it exactly how you said it:
str.match(/(^[^%]*$)|^([^%]*)%.*/i)
This will match any string without a '%' or the first part of a string that contains a %. You have to get the result from the 1st or 2nd group.
EDIT: This is exactly what you want below
str.match(/(?:^[^%]*$)|^(?:[^%]*)(?=%)/)
so the regex reads match any string that doesnt contain %, OR (otherwise match) all of the characters before the first %
How about the simpler
str.match(/[^%]*/i)[0]
Which means, match zero-or-more character, which is not a %
.
Edit: If need to parse until </a>
, then you could parse a sequence pf characters, followed by </a>
, then then discard the </a>
, which means you should use positive look-ahead instead of negative.
str.match(/.*?(?=<\/a>|$)/i)[0]
This means: match zero-or-more character lazily, until reaching a </a>
or end of string.
Note that *?
is a single operator, (.*)?
is not the same as .*?
.
(And don't parse HTML with a single regex, as usual.)