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

前端 未结 4 1351
天命终不由人
天命终不由人 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 16:56

    Given the use case of parsing a string, instead of having an actual DOM to work with, it does seem like regex is the way to go, unless you want to load the HTML into a document fragment and parse that.

    One way to get all of your matches is to make use of split:

    var htmlstr = "

    asdf@bsdf.com

    " var matches = htmlstr.split(/([A-Za-z.@]+)<\/a>/).filter((t, i) => i % 2)

    Using a regex with split returns all of the matches along with the text around them, then filtering by index % 2 will pare it down to just the regex matches.

提交回复
热议问题