Find and Replace String

Hi

I’ve been trying to get a script that will find and replace a word on the page but everything I’ve tried gets an Uncaught ReferenceError: str is not defined

Here’s my latest attempt:

<script>str = str.replace(/Free/gi, "Zero");</script>

I really need it to replace Free with 0.00 but I thought just for testing I would use Zero to be safe.

Thanks!

Where is str defined?

I don’t know. Looks like it’s defined right there i.e. str =

but that’s too late. It must be defined before the call to replace().

Reusing the same variable name can be confusing. Loosely translated, what that line is saying is:

Give the str variable the value of the str variable with the text “Free” replaced with the text “Zero”

Maybe it would be clearer like this:

let source_string = "Some Free text here"; 
let replaced_string = source_string.replace(/Free/gi, "Zero"); 

It still doesn’t work although there is no error.

I tried:

let source_string = "Free";let replaced_string = source_string.replace(/Free/gi, "Zero");

because the only work is “Free”. Here is the html:

<span class="price">Free</span>

I think it is actually being replaced already by some javascript somewhere. But I can’t locate it. I figured it would error or something if replacing it “again” didn’t work.

If you try

let source_string = "Some Free text here"; 
let replaced_string = source_string.replace(/Free/gi, "Zero"); 
console.log(replaced_string); 

you don’t see “Some Zero text here” in the console?

I’m guessing you’re not showing the code that’s responsible for the problem. i.e. getting the text to assign to the variable.

Yes, it shows it changed in the console log. But not on the page.

Here is the script that changes it from 0.00 to Free:

<script>etCurrencyManagerJsConfig={"precision":2,"position":8,"display":2,"zerotext":"Free","excludecheckout":"0","cutzerodecimal":"0","cutzerodecimal_suffix":",-","min_decimal_count":"2"};try{extendProductConfigformatPrice();}catch(e){}</script>

But I’ve remove the above off of the page that I’m trying to not have it changed on. But there’s more to the script that is in a .js file that I have combined with lots of other scripts.

Shouldn’t taking the part of the script off the page stop it from working?

I see. That does complicate things some.

  • script defines the etCurrencyManagerJsConfig object
  • the extendProductConfigformatPrice function is called (but no arguments are passed to it)
  • presumably the function uses the object values including inserting the zerotext value into the DOM
  • you want to change the value of “zerotext” as it appears in a page

If you can do so, I think the easiest way would be to change the objects value by either hardcoding in the alternative or by overriding it. eg.

etCurrencyManagerJsConfig={"precision":2,
        "position":8,
        "display":2,
        "zerotext":"Free",
        "excludecheckout":"0",
        "cutzerodecimal":"0",
        "cutzerodecimal_suffix":",-",
        "min_decimal_count":"2"};
etCurrencyManagerJsConfig.zerotext = "Zero";
try{
  extendProductConfigformatPrice();
}
catch(e){}

Only if I was unable to alter the value before it got put into the DOM would I bother doing string replacement. If the DOM insertion is happening on page load it should be easy enough to run the string replacement after it’s happened by running it afterwards. If extendProductConfigformatPrice involves XHR that does the DOM insertion after the page load then if possible some kind of callback would likely be a good solution.

I couldn’t get anything to work so I thought of a way to do it through php. Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.