javascript - string starts with an html tag -


can suggest way tell if first word in string html tag?

at moment, doing this:

var text = model.get('message');  try {   $(text)[0]; } catch (_error) {   text = text.replace(/\n/g, '<br />'); } 

but seems terribly inefficient.

i'd suggest done library you're using (jquery). here's extract source :

// simple way check html strings // prioritize #id on <tag> avoid xss via location.hash (#9521) // strict html recognition (#11290: must start <) rquickexpr = /^(?:\s*(<[\w\w]+>)[^>]*|#([\w-]*))$/, 

so do

 if (rquickexpr.test(text)) { // it's html 

note there's no guarantee it's really valid html.


Comments