Extract inner text from anchor tag string using a regular expression in JavaScript

前端 未结 4 1348
天命终不由人
天命终不由人 2021-01-13 16:38

I am new to angular js . I have regex which gets all the anchor tags. My reg ex is

/]*>([^<]+)<\\/a>/g
         


        
4条回答
  •  伪装坚强ぢ
    2021-01-13 17:07

    You need to capture the group inside the anchor tags. The regular expression already matches the inner group ([^<]+) But, when matching there are different ways to extract that inner text.

    When using the Match function it will return an array of matched elements, the first one, will match the whole regular expression and the following elements will match the included groups in the regular expression.

    Try this:

    var reg = /]*>([^<]+)<\/a>/g
    
    reg.exec(str)[1]
    

    Also the match function will return an array only if the g flag is not present.

    Check https://javascript.info/regexp-groups for further documentation.

提交回复
热议问题