Javascript regular expression: match anything up until something (if there it exists)

后端 未结 5 1704
盖世英雄少女心
盖世英雄少女心 2021-02-05 06:18

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

  • \"45%\", expected
相关标签:
5条回答
  • 2021-02-05 06:26

    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);
    }
    
    0 讨论(0)
  • 2021-02-05 06:28

    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];
    
    0 讨论(0)
  • 2021-02-05 06:29

    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".

    0 讨论(0)
  • 2021-02-05 06:37

    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(/(?:^[^%]*$)|^(?:[^%]*)(?=%)/)
    
    • The ?: removes all grouping
    • The ?= is a lookahead to see if the string contains %
    • and [^%] matches any character that is not a %

    so the regex reads match any string that doesnt contain %, OR (otherwise match) all of the characters before the first %

    0 讨论(0)
  • 2021-02-05 06:42

    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.)

    0 讨论(0)
提交回复
热议问题