最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Check if String is HTML Tag - Stack Overflow

programmeradmin2浏览0评论

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 = '&nbsp;' + $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 pletely; 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?!

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 = '&nbsp;' + $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 pletely; 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?!

Share Improve this question edited Jun 25, 2013 at 5:50 PRO MAX asked Jun 25, 2013 at 5:21 PRO MAXPRO MAX 874 silver badges11 bronze badges 2
  • Provide a sample string ... – Prasath K Commented Jun 25, 2013 at 5:29
  • I've added more details... – PRO MAX Commented Jun 25, 2013 at 5:50
Add a ment  | 

4 Answers 4

Reset to default 3

Something like this?:

var str = "<img alt='hello'>";

if(str.match(/\<img.+\>/)) {
    str = $(str).attr('alt');
}

It takes the string, checks if it's an img element, and then gets the alt attribute. A simpler way would be to just check for alt in the if statement:

if($(str).attr('alt')) {
    str = $(str).attr('alt');
}

You can also use is() and attr() bined:

if($(str).is('img') && $(str).attr('alt')) {
    str = $(str).attr('alt');
}

If you just want to check if it's an image and has an alt attribute try this regex:

/(?=.*alt=".+")<img.*?\/?>/

Quick test:

var str1 = '<img src="http://dot./test.jpg" alt="test"/>';
var str2 = '<img src="http://dot./test.jpg" alt="" />';
var str3 = '<input/>';

console.log(img.test(str1)); //= true
console.log(img.test(str2)); //= false
console.log(img.test(str3)); //= false

Does this help ?

Demo

var samples = ["<img src='#' alt='img1' />","hello","bye","<img src='#' alt='img2' />"];
for(var i = 0, len = samples.length; i < len; i++) {
    if($(samples[i]).length > 0) {
        alert($(samples[i]).attr('alt'));
    }    
}

I found the problem myself... It was a logical error...

the point is that when the string is a normal text, we can take it with using text(), but when it is HTML we should use html() instead, otherwise the text() returns null...

I've also learned something from you guys... Thank you all very much... ;)

发布评论

评论列表(0)

  1. 暂无评论