skip to Main Content

I want to find the text that starts with any character(but not space) and ends with space
I can use S+s

wonders like <a href="https://example" title="Perito Moreno Glacier">Perito Moreno Glacier</a> and

The issue is that the a link element is also matched, so I want to skip the a element for this searching

I can use <a.*?a> for searching a element.

So I want to combine the logic S+s but skip <a.*?a>

I have tried (S+s)|(<a.*?a>) but it didn’t work.
The expected result is
wonders
like
<a href="https://example" title="Perito Moreno Glacier">Perito Moreno Glacier</a>
and

2

Answers


  1. As implied in the comments about regexp and html… I will use dom instead. Just in case your string may contain nested html.

    However, if your string is a flat and valid html, you could also have a look at this answer which is about extracting words or "phrases".

    const string = 'wonders like <a href="https://example" title="Perito Moreno Glacier">Perito Moreno Glacier</a> and';
    
    // let's hope string is valid html
    var html = "<span>" + string + "</span>"
    
    // convert it to HTMLElement (could use DOMParser instead)
    var dummy = document.createElement("div");
    dummy.innerHTML = html
    var node = dummy.firstChild;
    
    function processNode(node) {
      var all = [];
      for (node = node.firstChild; node; node = node.nextSibling) {
        if (node.nodeType == 3) {
    
          // text node
          var str = node.nodeValue
          all = all.concat(str.trim().split(/s+/))
        } else {
    
          // an element
          all.push(node.outerHTML)
          
          // or instead, let's get fancy:
          // all = all.concat(processNode(node))
        }
      }
      return all;
    
    }
    
    console.log(processNode(node))
    Login or Signup to reply.
  2. Maybe this?

    <a[^>]+>[^<]+</a>|w+
    

    Explanation

    this part matches the whole link:
    <a[^>]+>[^<]+</a>
    
    this part means "or a word":
    |w+
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search