I just need to check if a string is a HTML Tag or not. I've searched in google and tried codes below, but no success:
var v = $(string).html() ? 1 : 0;
--or----------------------------------------------
var htmlExpr = new RegExp("/^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/");
var v = htmlExpr.test(string) ? 1 : 0;
--or----------------------------------------------
var v = $(string).each(function(){$(this).html();} ? 1 : 0;
====
Actually I want to check if the string is an img tag, get the alt attribute of it and if is not (it's a normal non-html string) get the whole of it.
Please help me with this problem... Thanks
====
Let me show you a true example with all side details...
This is the main function:
content.find('a').each(function () {
var $this = $(this),
optText = ' ' + $this.text(),
...,
...,
...;
...
...
...
}
in this function, I need to process the "$this.text()" part in the 3rd line.
The text in a tag which we are looking for, sometimes contains a normal string and sometimes includes an img tag. When it is a normal text, I want to put it in optText variable completely; But when it is an img tag, I just want to put it's alt attribute in that variable.
So briefly:
$this.text() => NormalText
--or----------------------------------------------
$this.text() => <img src="#" alt="AltText" />
So I've changed that function and replaced the "$this.text()" code in the third line with each of these codes:
($this.text().html() ? $('img', $this.text()).attr('alt') : $this.text())
--or----------------------------------------------
(htmlExpr.test($this.text()) ? $('img', $this.text()).attr('alt') : $this.text())
[which htmlExpr was defined before...]
--or----------------------------------------------
($this.text().each(function(){$(this).html();}) ? $('img', $this.text()).attr('alt') : $this.text())
but none of these codes worked for me...
Now, Can you help me please?!