Regex to match not start of line

前端 未结 6 619
陌清茗
陌清茗 2021-02-02 16:08

I have the following XML tag


I want to replace the < in the text with <

相关标签:
6条回答
  • 2021-02-02 16:44

    [^<]+ = one or more characters that are not <

    < = the < you're looking for

    replace:

    ([^<]+)<
    

    with:

    $1&lt;
    
    0 讨论(0)
  • 2021-02-02 16:50

    This would give you "<" after the first instance:

    [^<]<
    
    0 讨论(0)
  • 2021-02-02 16:51

    Try this:

    (?<="[^"]*)<(?=[^"]*")
    
    0 讨论(0)
  • The dot '.' means "any value"

    .<
    

    Anyway, I suppose you don't want whitespaces, either. If so, then

    \S\s*<
    
    0 讨论(0)
  • 2021-02-02 17:02

    @duncan's method works fine if you just want to replacing, but it doesn't match the <. and all the lookbehind wont work if you a using javascript. because javascript doesn't support lookbehind, unless you turn on the --harmony flag in nodejs or experimental javascript features in chrome. But lookahead will work here, which is: /(?!^)</ will match the < which is not at the begining of a line. and the replacing will be: '<list message="2 < 3">'.replace(/(?!^)</, '&lt;')

    0 讨论(0)
  • 2021-02-02 17:10

    Most likely you can do this using lookbehind:

    /(?<!^)</
    

    see: http://www.regular-expressions.info/lookaround.html

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