删除<nobr> </nobr>字符串

时间:2010-10-18 16:21:47

标签: jquery

我有以下变量,其中包含以下字符串。我想删除<nobr></nobr>,然后留下内部的内容。

alert(product_code);

生成

<nobr>209/00254/01</nobr>

如何创建一个删除<nobr>标签的新变量?

var product_code = $(this).html();
alert(product_code);

5 个答案:

答案 0 :(得分:4)

var product_code = $(this).text();

答案 1 :(得分:2)

尝试:

$(this).text();

$(this).children("nobr").html();

答案 2 :(得分:2)

如果您想完全删除它们(从原始版本),您可以使用.replaceWith()例如:

$("nobr").replaceWith(function() { return $(this).contents() });

You can test it out here。或者,$.trim() .text()结果(因为它们在结果中是空格):

var product_code = $.trim($(this).text());

答案 3 :(得分:1)

如果你有多个:

$(“nobr”,this).each(function(){

$(this).replaceWith(function(){return $(this).contents()});

});

答案 4 :(得分:0)

如果代码真的那样,你可以使用普通的javascript:

来完成
product_code = '<nobr>209/00254/01</nobr>';

// using simple string manipulation
alert( product_code.substring( 6, product_code.length - 7 ) );

// using regular expressions
alert( product_code.match( '<nobr>(.*)</nobr>' )[1] );

// using a bit more powerful regular expression
result = product_code.match( '<nobr>((\\d+)/(\\d+)/(\\d+))</nobr>' );
alert( result[1] ); // 209/00254/01
alert( result[2] ); // 209
alert( result[3] ); // 00254
alert( result[4] ); // 01
相关问题