-2

Is it possible (maybe using RegExp or something else) to do the following with less code?

var bTest = szNextEntity[0] == '<' && szNextEntity[1] != '/'

So bTest is true if the zeroth character of szNextEntity == '<' and the first character != '/'

Am interested in how concise this can be made.

Thanks.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
user3738290
  • 445
  • 3
  • 16
  • 2
    regex would be /^<[^\/]/ – ControlAltDel Sep 10 '21 at 13:15
  • XML is not a regular language. Regular expressions are not the right tool for this job. [Have you tried using an XML parser instead?](/a/1732454/4642212) Use [`DOMParser`](//developer.mozilla.org/docs/Web/API/DOMParser): `new DOMParser().parseFromString(` _your string here_ `, "text/xml");`. The same applies for HTML. – Sebastian Simon Sep 10 '21 at 13:22

1 Answers1

1

You could use a little regex to test the string. But in general, using regex to parse HTML is a bad idea.

Even Jon Skeet cannot parse HTML using regular expressions. Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing HTML with regex summons tainted souls into the realm of the living. HTML and regex go together like love, marriage, and ritual infanticide.

var re = /^<[^\/]/;

var str1 = '</end tag>';
var str2 = '<start tag>';

console.log(re.test(str1));
console.log(re.test(str2));
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116