I would like to remove a wildcard string of text from an existing string, then append it to after a div.
In my case, I'd like to find a part number from within a string, and then set that string to a variable.
So instead of this (please note between [] can be anything):
<div> The part number is [8F8S0JJ9]</div>
<div> The second part number is [V9S7D73]</div>
I would have:
<div> The part number is</div>
[8F8S0JJ9]
<div> The second part number is</div>
[V9S7D73]
Here's my code:
// Get the product number that lies between [ ] marks from all div elements
$('div:contains('['+*+']')').html(function() {
//Look for the wildcard string and save it to a variable. how can I search within the string?!
var $finalstring = $(this).search('['+*+']');
//remove it from the string
$(this).replace($finalstring, '');
//Set it to display after the string
$(this).append($finalstring);
});
Thanks
I would simplify things and just use a class:
$('.part').each(function(){
var $this = $(this),
partnum_reg = /(\[(.)+\])/g,
partnum = String($this.html().match(partnum_reg));
$this.html($this.html().replace(partnum, ''));
$('<div/>').insertAfter($this).html(partnum);
});
Demo: http://jsfiddle.net/hB4Zp/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With