0

I need to replace some text that is on the page within the body tag. I am using javascript but have jquery available if needed. I basically need to replace test® (test with the registered trademark) with TEST® or tests® with TESTS® and it could even be test with TEST® or tests with TESTS®. I am able to uppercase them but its not liking to work for me with the ® sign, it wants to put duplicates on ones that already have it. Basically anything on the page that has the word test or tests should be TEST® or TESTS® if it is plural. Any help is appreciated.

EDIT:
So now I have this:

var html = $('body').html();
var html = html.replace(/realtor(s)?(®)?/gi, function(m, s1, s2){
    var s = s1?s1.toUpperCase():"";
    var reg = s2?s2:'®';
    return "REALTOR"+s+reg;
});
$('body').html(html);

Its working well other than it is duplicating the ® on the ones that already had them any ideas on how not to?

2
  • just google javascript text replace , first link comes up tizag.com thats quite enough to accomplish so , good point Pointy Commented Mar 31, 2010 at 13:02
  • 1
    The problem with using ® or any HTML escape code is that the browser translates it to the actual symbol. To get to that symbol using string functions, use "®" but if you are using match, replace, etc, you will have to use the unicode value "\u00ae". See my answer below. Commented Mar 31, 2010 at 14:09

4 Answers 4

3

As others have already said, you will not be able to match the ®, you need to match on
\u00ae.

The code you provided needs to be changed to:

var html = $('body').html();
var html = html.replace(/realtor(s)?(\u00ae)?/gi, function(m, s1, s2){
    var s = s1?s1.toUpperCase():"";
    var reg = s2?s2:'®';
    return "REALTOR"+s+reg;
});
$('body').html(html);
Sign up to request clarification or add additional context in comments.

Comments

1

To expand on jAndy's answer, try this:

 $("div, p, span").each(function(){
  o = $(this);
  o.html( o.text().replace(/test(|s)\u00ae/gi, function($1){
     return($1.toUpperCase());
  }));
 });

Using the code you provided, try this:

$(document).ready(function(){
 $('body').html( $('body').html().replace(/realtor(|s)\u00ae/gi, function($1){
  return($1.toUpperCase() );
 }));
})

3 Comments

that works great the only problem is that if there is no ® at the end (for example realtor) it doesnt add the ® in there. I need it to do that. Any ideas?
Gave you a plus one because you helped actually get the solution.
The way your question is written it seems that the ® is already there, so this replace function will find all instances of realtor® or realtors® and make it uppercase, so you don't need to add another "®". Anyway, glad you have your solution and thanks for the upvote.
0

Instead of creating something from scratch try using an alternate library. I develop with PHP so using a library that has identical methods in JavaScript is a life saver.

PHP.JS Library

var newReplaced = $P.str_replace("find","replace",varSearch);

2 Comments

-1 (you just can't see it... cause I'm out of votes). Self promoting is always bad, but yours is terrible.
I use this library in conjunction with jQuery. No real problems.
0

The tricky part here is to match the ®, which is a Unicode character, I guess... Have you tried the obvious?

var newStr = str.replace(/test(s)?®?/gi, function(m, s1){
  var s = s1?s1.toUpperCase():"";
  return "TEST"+s+"®";
});

If the problem is that ® does not match, try with its unicode character number:

/test(s)?\u00ae/

Sorry if the rest does not work, I assume your replacement already works and you just have to also match the ® so that it does not get duplicated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.