tags:

views:

64

answers:

5

I have the following variable which contains the following string. I would like to remove both the <nobr> and the </nobr> and just leave what is inside.

alert(product_code);

produces

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

How would I make a new variable that removes the <nobr> tags?

var product_code = $(this).html();
alert(product_code);
+2  A: 

try either:

$(this).text();

or

$(this).children("nobr").html();
Michael Gattuso
I tried the first but that just gives me the text but with blank spaces after it. Will try second answer.
This would result in a syntax error...
Nick Craver
javscript error on the second response. XML filter is applied to non-XML value ({0:#1=({}), context:#1#, length:1})
You could always trim the string with the first answer.
Michael Gattuso
Forget to add the call to the children method on the second solution.
Michael Gattuso
+2  A: 

If you wanted to just remove them completely (from the original), you can use .replaceWith() for example:

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

You can test it out here. Or, $.trim() the .text() result (since they'll be spaces in the result):

var product_code = $.trim($(this).text());
Nick Craver
as always Nick you come thru with the specific, simple, answers that work exactly!!! product_code = $.trim($(this).text()); is exactly what I needed.
+4  A: 
var product_code = $(this).text();
LukeH
A: 

If the code is really just like that, you can do this with plain 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
poke
A: 

in case you have more than one:

$("nobr", this).each(function(){

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

});

nopuck4you