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

前端 未结 4 1493
天命终不由人
天命终不由人 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条回答
  •  猫巷女王i
    2021-01-13 17:19

    Why are you trying to reinvent the wheel?

    You are trying to parse the HTML string with a regex it will be a very complicated task, just use DOM or jQuery to get the links contents, they are made for this.

    • Put the HTML string as the HTML of a jQuery/DOM element.

    • Then fetch this created DOM element to get all the a elements inside it and return their contents in an array.

    This is how should be your code:

    var str = 'abc.jagadale@gmail.com';
    
    var results = [];
    $("
    ").html(str).find("a").each(function(l) { results.push($(this).text()); });

    Demo:

    var str = 'abc.jagadale@gmail.com';
    
    var results = [];
    $("
    ").html(str).find("a").each(function(l) { results.push($(this).text()); }); console.log(results);

提交回复
热议问题