jQuery:使用正则表达式在textarea值中搜索单词 [英] jQuery: Searching textarea value for words using regex
本文介绍了jQuery:使用正则表达式在textarea值中搜索单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
<script type="text/javascript">
$(function(){
var txt = new Array();
var count = 0;
$('textarea[id*="page_parts_attributes_"]').each(function(){
var html = $(this).html($(this).val());
var matches = $(html).match(/\b/g)
if(matches) {
txt[count] = jQuery.trim(matches).length / 2
count++;
}
});
var final_count = eval(txt.join('+'));
console.log(Math.floor(final_count));
})
</script>
我基本上是想在文本区域搜索单词.我必须将textarea的值转换为HTML,然后我要在HTML中搜索单词...但是它不起作用...
I am basically trying to search the textarea for words. I have to convert the value of the textarea to HTML, then I want to search the HTML for words... but its not working...
有什么想法吗?
推荐答案
var html = $(this).html($(this).val());
var matches = $(html).match(/\b/g)
应该是
var matches = $(this).val().match(/\b/g);
这篇关于jQuery:使用正则表达式在textarea值中搜索单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文