Reputation: 41
I have this code
<div class="new">
<label> text text text (=price) </label>
</div>
I want to remove the "(=" and ")" around the price,
I've tried the following but to no avail:
jQuery(document).ready(function() {
jQuery(".new label").each(function() {
jQuery(this).text(jQuery(this).text().replace(/[(=]/, ""));
});
});
Upvotes: 1
Views: 4677
Reputation: 8941
Nobody has mentioned the 'g' flag, which will replace everything globally:
$(".new label").each(function() {
$(this).text($(this).text().replace(/[\)\(=]?/g, ""));
});
You also needed to escape the parentheses.
edit I won't delete this answer, but it shouldn't be used, because now that I re-read it, it's obvious that it will replace any parentheses or equals signs in the entire label's text. Use Ben Lee's solution.
Upvotes: 0
Reputation: 26601
This jsfiddle will match the regexp and replace the content with price
(in my example, but you could do whatever you want).
Here is the js:
jQuery(document).ready(function() {
jQuery(".new label").each(function() {
jQuery(this).text(jQuery(this).text().replace(/\(=([A-z]*)\)/, "$1"));
});
});
Explanation
[A-z]
).\(=
and /)
(don't forget to escape parenthesis)$1
(this is the first set between parenthesis (...)
)See Alternation and grouping paragraph on this page
Upvotes: 0
Reputation: 53349
I think your regex pattern is off. Try this:
jQuery(this).text(jQuery(this).text().replace(/\(=(.*?)\)/, "$1"));
Upvotes: 5
Reputation: 35793
jQuery(".new label").each(function() {
jQuery(this).text(jQuery(this).text().replace("(=", "").replace(")", ""));
});
This assumes that (= and ) wont appear anywhere else in the label.
Upvotes: -1