Reputation: 1089
I have a two text strings in a span class and I need to remove a part of it with either jQuery or vanilla Javascript. The string is #AAA and #AAB
I have used a few variations before for achieving this result, but on this span class it doesn´t seem to work.
This is the script I've tried:
$j('document').ready(
function() {
$j(”span.sku").text(function() {
return $(this).text().replace(/(#AAA |#AAB)/g, "");
}).addClass(”remove");
});
and
$j('body').ready(function(){
$j( ”span.sku:contains('#AAA')" ).addClass( ”remove" );
$j( ”span.sku:contains('#AAB')" ).addClass( ”remove" );
});
The last one I tried to just set a class that I could hide with CSS.
The $j is for non-conflict mode that the site is using.
The html:
<div class="product_meta">
<span class="sku_wrapper">SKU: <span class="sku" itemprop="sku">K1J2AEA#AAA</span></span>
<meta itemprop="brand" content="HP">
<meta itemprop="gtin14" content="">
<meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition">
<span class="posted_in">Categorie: <a href="/category/computers/notebooks/" rel="tag">Notebooks</a></span>
</div>
Upvotes: 0
Views: 333
Reputation: 6644
You don't even need to use regex for this:
var str1 = 'bananarama';
var str2 = 'rama';
str1 = str1.replace(str2, '');
document.body.innerHTML = str1;
Upvotes: 0
Reputation: 388316
You need to use '
or "
for string literals also need to remove the (space ) in the regex
var $j = jQuery.noConflict(true);
$j(function($) { //dom ready handler receives a jQuery object reference as its parameter so you can assign it ti $ and use it inside the handler
$("span.sku").text(function(i, text) {
return text.replace(/#AAA|#AAB/g, "");
}).addClass('remove');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="product_meta">
<span class="sku_wrapper">SKU: <span class="sku" itemprop="sku">K1J2AEA#AAA</span></span>
<meta itemprop="brand" content="HP">
<meta itemprop="gtin14" content="">
<meta itemprop="itemCondition" itemtype="http://schema.org/OfferItemCondition" content="http://schema.org/NewCondition">
<span class="posted_in">Categorie: <a href="/category/computers/notebooks/" rel="tag">Notebooks</a></span>
</div>
Upvotes: 1