Others

How to Remove a String Prefix from Another String in JavaScript

Phi Hung VoPhi Hung Vo Nov 5, 2026 869 views

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.

How to Remove a String Prefix from Another String 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.
Before you start

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.

Solution 1Recommended

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.

1
Declare your variables

Start by defining your prefix in one variable and the full string in another. For example: var a = 'sysid.'; and var b = 'sysid.23';

2
Apply the replace() method

Call the replace function on your target string to substitute the prefix with an empty string: var result = b.replace(a, '');

3
Output the result

The 'result' variable will now hold the value '23'. You can log this to the console or write it back to your spreadsheet cell.

Single Occurrence Note: The standard replace() method only replaces the first occurrence of the matched string, which is perfect for removing a prefix.
WPS JS Macros

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. 1. Open WPS Spreadsheet: Launch WPS Office and open the spreadsheet containing the data you want to clean.
  2. 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. 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. 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.
Native support for JavaScript (JSA) string manipulation functionsSeamless format compatibility with Microsoft Excel filesEasily automate repetitive data cleaning and prefix removal tasksLightweight and completely free to use
microsoft office alternative - wps office

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.