javascript 使用jquery根据<br/>标签拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18592144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split the string based on <br/> tag using jquery
提问by Vignesh Pichamani
How can i split the string containing <br/>
tag using jquery. I tried the following code but it get error in console. I am not sure how to split the string based on <br/>
tag Here is the code what i tried
如何<br/>
使用 jquery拆分包含标签的字符串。我尝试了以下代码,但在控制台中出现错误。我不知道如何根据<br/>
标签拆分字符串这是我尝试过的代码
jQuery(document).ready(function($)
{
var lines = jQuery('this is for testing <br/> How are you<br/>').split('<br/>');
jQuery.each(lines, function() {
alert(this);
});
});
Any suggestion would be great.
任何建议都会很棒。
回答by xr280xr
Lots of duplicate answers here. This one is different. If you can guarantee the spelling of the <br/>
tag, the other answers are fine. But if you have no control over the HTML, the line break tag could come in different formats:
这里有很多重复的答案。这个不一样。如果你能保证<br/>
标签的拼写,其他答案都可以。但是,如果您无法控制 HTML,则换行标记可能有不同的格式:
<br/>
<BR/>
<br />
<br>
<br >
...etc.
<br/>
<BR/>
<br />
<br>
<br >
...等。
major browsers can all handle all of these, but only the first will be handled by the suggested .split("<br/>")
operation. A more robust option is to use a regular expression to match the tag:
主要浏览器都可以处理所有这些,但只有第一个会被建议的.split("<br/>")
操作处理。更强大的选择是使用正则表达式来匹配标签:
jQuery(document).ready(function($)
{
var brExp = /<br\s*\/?>/i;
var lines = ("this is for testing <br/> How are you<BR />").split(brExp);
});
I've written the expression to be case-insensitive, allow any number of spaces after '<br', and the '/' is optional.
我已经编写了不区分大小写的表达式,在“<br”之后允许有任意数量的空格,“/”是可选的。
回答by Alex K.
You want to split a vanilla string, don't pass it to $()
rather simply;
你想分割一个香草字符串,不要$()
简单地将它传递给;
jQuery(document).ready(function($)
{
var lines = 'this is for testing <br/> How are you<br/>'.split('<br/>');
jQuery.each(lines, function() {
alert(this);
});
});
回答by Dutts
What if you try:
如果你尝试:
jQuery(document).ready(function($)
{
var lines = 'this is for testing <br/> How are you<br/>'.split('<br/>');
each(lines, function() {
alert(this);
});
});
回答by Indra Yadav
Here is the Working code
这是工作代码
jQuery(document).ready(function($)
{
var lines = 'this is for testing <br/> How are you<br/>'.split('<br/>');
alert("workig");
jQuery.each(lines, function() {
alert(this);
});
});
回答by x-yuri
That's no jQuery solution, but hopefully somebody will find it useful. The function returns one or two elements.
这不是 jQuery 解决方案,但希望有人会发现它有用。该函数返回一或两个元素。
function splitContentsOnBr(el) {
const before = document.createElement('div');
const after = document.createElement('div');
let found = false;
el.childNodes.forEach(c => {
if (found) {
after.appendChild(c.cloneNode(true));
} else if (c.tagName == 'BR') {
found = true;
} else {
before.appendChild(c.cloneNode(true));
}
});
return after.childNodes.length ? [before, after] : [before];
}
document.querySelector('#result').innerHTML = splitContentsOnBr(document.querySelector('h1'))
.map(el => el.textContent.trim())
.join(', ');
<h1>
First part
<br>
Second part
</h1>
<div id="result">
</div>
回答by Dimitar Dimitrov
Can't you just do something like:
你不能做这样的事情:
var lines = 'this is for testing <br/> How are you<br/>'.split('<br/>');
Just as a side note if you want to get rid of the empty elements you can filter the lines
like so:
作为旁注,如果您想摆脱空元素,您可以lines
像这样过滤:
// this will get rid of empty elements
lines = lines.filter(function(n) {
return n;
});
回答by Bindiya Patoliya
jQuery(document).ready(function($)
{
var str = 'this is for testing <br/> How are you<br/>';
var lines = str .split('<br/>');
jQuery.each(lines, function() {
alert(this);
});
});