How to Remove a String Prefix from Another String in JavaScript
Question details
The user wants to remove a specific string prefix (such as 'sysid.') from another string (such as 'sysid.23') to extract the remaining numerical or text value in JavaScript.

- Product
- JavaScript / WPS Spreadsheet JS Macros
- Device & OS
- not provided
- Scenario
- Cleaning up dynamically generated data by stripping away unnecessary known prefixes to isolate the actual data values.
- Observed behavior
- The intended behavior is to efficiently subtract or strip the defined prefix from the target string and return only the remaining suffix value.
Identify the exact prefix string you need to remove and ensure your JavaScript environment, such as the WPS Macro Editor, is open and ready to test your script.
Use the JavaScript replace() Method
The most straightforward way to remove a specific prefix from a string is by using the replace() method to swap the known prefix with an empty string.
The replace() method searches a string for a specified value and returns a new string where the specified values are replaced. By replacing the prefix with an empty string, you effectively delete it from the result.
Start by defining your prefix in one variable and the full string in another. For example: var a = 'sysid.'; and var b = 'sysid.23';
Call the replace function on your target string to substitute the prefix with an empty string: var result = b.replace(a, '');
The 'result' variable will now hold the value '23'. You can log this to the console or write it back to your spreadsheet cell.
Use the JavaScript split() Method
If your string format is highly consistent and always separated by a specific character (like a period), you can split the string into an array and grab the desired element.
Automate String Manipulation in WPS Spreadsheet Using JS Macros
WPS Office natively supports JavaScript for Applications (JSA). You can use the exact JavaScript replace() and split() methods described above to clean up data, remove text prefixes, and automate workflows directly within your spreadsheets.
- 1. Open WPS Spreadsheet: Launch WPS Office and open the spreadsheet containing the data you want to clean.
- 2. Access the Macro Editor: Navigate to the 'Tools' tab on the ribbon, click on 'WPS Macro', and select 'Macro Editor' to launch the JSA environment.
- 3. Write your JavaScript code: Create a new module and write a loop that applies the replace() method to the cells containing the prefixes.
- 4. Run the automation: Click the 'Run' button in the Macro Editor to automatically process the text and strip the prefixes from your worksheet data.

Frequently Asked Questions
Does the replace() method alter the original string?
No, strings in JavaScript are immutable. The replace() method returns a completely new string with the modifications, leaving the original string variable unchanged.
What happens if the prefix isn't found in the string?
If the specified prefix or substring is not found within the target string, the replace() method will simply return the original string completely unmodified.
Can I remove multiple different prefixes at once?
Yes, you can chain multiple replace() methods together, or use a Regular Expression (RegEx) with the replace() function to match and remove various prefixes in a single line of code.
Is it possible to use JavaScript to clean data in WPS Office?
Absolutely. WPS Office features a robust Macro Editor that uses JavaScript (JSA) instead of VBA. You can write custom JavaScript scripts to manipulate text, clean strings, and automate your spreadsheets.




