Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Re: String.replace() returning unexpected result when replacement string undefined

Solved
Jump to Solution
1066 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Zollie
10 - Mercury
10 - Mercury

Doing some simple substring removal and getting different results from the block than in JS Bin.

let renderName = "MX_Topo-Chico-Agua_Mineral-Water_600ml_PET_6x";
let secondaryQuantity = "6x";

// scripting block generates an UNEXPECTED output: "MXTopo-Chico-Agua_Mineral-Water_600ml_PET"
output.text( removeSubString(renderName,secondaryQuantity) );

// JS Bin, however, outputs as EXPECTED: "MX_Topo-Chico-Agua_Mineral-Water_600ml_PET_"
// console.log( removeSubString(renderName,secondaryQuantity) );

function removeSubString(str,substr) {
    return str.replace(substr,"");
}

Oddly, making use of a character (rather than “”) yields proper results.

renderName.replace(secondaryQuantity,"1")
=> "MX_Topo-Chico-Agua_Mineral-Water_600ml_PET_1"
1 Solution

Accepted Solutions
Stephen_Suen
Community Manager
Community Manager

Quick update on this — the unexpected behavior has been fixed. output.text will no longer apply Markdown formatting (use output.markdown for that).

See Solution in Thread

2 Replies 2
Shrey_Banga
6 - Interface Innovator
6 - Interface Innovator

Ah this is because output.text supports Markdown. So it’s italicizing _Topo-Chico-Agua_. I’d suggest either wrapping the input in backticks, e.g. output.text('`' + removeSubString(renderName,secondaryQuantity) + '`' ); or using console.log if you’re just printing out for debugging.

Stephen_Suen
Community Manager
Community Manager

Quick update on this — the unexpected behavior has been fixed. output.text will no longer apply Markdown formatting (use output.markdown for that).