我在页面上有一些文本,我想找到并删除括号中的任何文本。 例如:在jQuery中查找包含在括号中的文本
<td>here is [my text] that I want to look at</td>
所以我想抓住该文本(我的文本),它保存在一个变量,并从那里将其删除。
我在页面上有一些文本,我想找到并删除括号中的任何文本。 例如:在jQuery中查找包含在括号中的文本
<td>here is [my text] that I want to look at</td>
所以我想抓住该文本(我的文本),它保存在一个变量,并从那里将其删除。
如果你使用jQuery,你可以在$('body')。text()上使用正则表达式,如\[(.+)\]
。
编辑:对不起,我可能会跳枪给你一个这个答案。继续考虑更多几分钟,然后尝试用更多的信息来更新它。
你可能会发现这个任务不是那么简单。如果你在文本上的控制,在它发送到Web浏览器,你可以把它放到一个<span class='bracket'>[my text]</span>
中的文本,那么你可以很容易地做这样的事情用jQuery:
$(".bracket").each(function() {
// store the data from $(this).text();
}).remove();
这可以通过定期做表达式和jQuery的,但也有可能攀升处理像<input name='test[one][]' />
属性中的文本问题的“简单”的正则表达式将做这样的事情:
$("td").each(function() {
var $this = $(this);
var html = $this.html();
var bracketText = [];
// match all bracketed text in the html - replace with an empty string
// but push the text on to the array.
html = html.replace(/\[([^\]]+)\]/g, function() {
bracketText.push(arguments[1]);
return "";
});
// put the new html in away and save the data for later
$this.html(html).data("bracketText", bracketText);
});
有没有,如果你这样做太大的危险'确保你不会在文本标签内部有[]
。
最后我做了以下内容:
$('#formQuizAnswers td.question').each(function(){
var header = $(this).text().match(/-.*-/);
$(this).text($(this).text().replace(header,''));
});
我改变了我的文字我搜索到有破折号周围IE -My文本的
我不会在这里使用贪婪的量词。那怎么样 - '/ - [^ - ] * - /' – kangax 2009-08-20 04:31:09
这是什么样的我在想,除了试图想想如何将它推广到不仅仅是一组特定的标签(如果你不知道包含括号内文本的标签是什么)。非常好。 – theIV 2009-08-20 01:54:17