ANS-0644 · SUITESCRIPT DEVELOPMENT

How to Replace All Occurrences of a Substring in NetSuite SuiteScript

Learn effective JavaScript methods for global string replacement within NetSuite SuiteScript, considering compatibility across different environments.

Short answer

To replace all instances of a specific word or substring in JavaScript within NetSuite SuiteScript, utilize string manipulation methods. The traditional approach involves a global regular expression with string.replace(), while modern JavaScript offers the replaceAll() method, though its compatibility in all SuiteScript versions may vary. Developers should test for environment support.

Scenario

NetSuite users often encounter situations requiring the replacement of all occurrences of a particular substring within a larger string using JavaScript. This is a common task in SuiteScript for data manipulation, formatting, or cleansing operations, where a simple one-time replacement is insufficient.

Solution

To replace all instances of a specific word or substring in JavaScript, the following expression can be used:

javascript
string.replace(/yourword/g,'new word');

This method utilizes a regular expression with the global flag (g) to ensure all matches are replaced. While this approach remains fully functional and widely supported across various JavaScript environments, including many NetSuite SuiteScript versions, modern JavaScript also introduced the replaceAll() method for a more direct syntax.

However, it is important to note that the replaceAll() method may not be universally supported in all NetSuite SuiteScript environments, particularly in older SuiteScript versions. Developers should test for compatibility if opting for replaceAll().

Expert NetSuite Support

Need help with this NetSuite issue?

SuiteScript Development consulting and configuration support

Talk to a consultant