admin管理员组文章数量:1426907
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 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 = ' ' + $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
4 Answers
Reset to default 3Something 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... ;)
本文标签: javascriptjQuery Check if String is HTML TagStack Overflow
版权声明:本文标题:javascript - jQuery Check if String is HTML Tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745487788a2660460.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论