Robin
Robin

Reputation: 5427

Regex - check string has one or more chars without some chars

I have a string like that

const str = "<p><p><br></p>something</p>";

I want to check if the string has one or more char without <, >, p, /, b, r, (whitespace). In the above case it should true, because the string has char of s, o, m, e e.t.c

How can do this check ?

Upvotes: 0

Views: 44

Answers (3)

Sohail Ashraf
Sohail Ashraf

Reputation: 10604

You could use lookaround in regex.

Lookahead and lookbehind, collectively called “lookaround”, are zero-length assertions just like the start and end of line, and start and end of word anchors.

Example.

  let regex = /(?<!<)[\w](?!>)/g;
const str = "<p><p><br></p>something</p>";

if(regex.test(str)) {
  console.log('Yes');
} else {
  console.log('No');
}
console.log('Match: ', str.match(regex).toString());

OR just use simple Character ranges.

    let regex = /[^pbr<>\/]/g;
    const str = "<p><p><br></p>something</p>";
    if(regex.test(str)) {
      console.log('Yes');
    } else {
      console.log('No');
    }
    console.log("Match: ", str.match(regex).toString());

Upvotes: 1

Robin
Robin

Reputation: 5427

I just figure out with lodash's pullAllBy function

let arr = "<p><p><br></p>somebbthing</p>"

const s1 = arr.split('');

let newArr = _.pullAllBy(s1, ['<', '>', '/', 'p', 'b', 'r']);
if(newArr.length > 0) {
  console.log('true');
}

Upvotes: 0

sa_n__u
sa_n__u

Reputation: 336

You can extract the text from html and check like below:

    

    function extractContent(s) {
      var span = document.createElement('span');
      span.innerHTML = s;
      return span.textContent || span.innerText;
    };
        
    cont = extractContent("<p><p><br></p>something</p>");
    if(cont)
      console.log(cont, true)

Upvotes: 1

Related Questions