Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In jQuery how do you save a specific wildcard string to variable?

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

like image 775
user1139861 Avatar asked Feb 22 '26 13:02

user1139861


1 Answers

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/

like image 121
AlienWebguy Avatar answered Feb 25 '26 02:02

AlienWebguy