3

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

5 Answers5

1

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/

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
0

If you don't want to use a combination of the string.indexOf('') and string.substring('') then try the following plugin linked to in this answer:

jQuery selector regular expressions

Community
  • 1
  • 1
Alex
  • 34,899
  • 5
  • 77
  • 90
0

Firstly, you can't simply do that with jQuery selectors I believe. Therefore you need to build your own filter. Using a simple regex, you can get the same effect. Secondly, you then need to remove the existing content. Lastly, you need to insert this content after the original div. You have some incorrect syntax, but here's a way to do it:

http://jsfiddle.net/zbE2F/

$("div").filter(function(index){
    return /\[.*\]/.test($(this).html());
}).each(function() { 
    var $finalstring = $(this).html().match(/\[.*\]/)[0];
    $(this).html($(this).html().replace(/\[.*\]/),"");
    $("<div></div>").insertAfter(this).append($finalstring);
});

hopefully this will get you closer to what you were needing, but note that if at any time a div contains two of the targeted strings, you will have problems.

Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
0

I'll add my solution as well even though it's basically the same as Joseph's haha.

http://jsfiddle.net/PjdXr/

var reg = /\[.*\]/;
$("div").filter(function(){
    return $(this).text().match(reg);  
}).html(function(i,h) { 
    var nr = h.match(reg);
    $(this).after(nr[0]);
    return h.replace(reg,'');
});
Andy
  • 29,707
  • 9
  • 41
  • 58
0

Here is a possible solution which assumes the wildcard is always the last word in the sentence :

http://jsfiddle.net/Aq9qe/

<div class='str'>Here is the [wildcard]</div>

var str = $('.str').html();
var split = str.split(' ');

var wild = split.pop();

$('.str').html(str.replace(wild, ' '));
$('.str').after(wild);
DaveE
  • 1,643
  • 2
  • 16
  • 29