JXtension - String.replace(target, replacement, limit, start)

Purpose:
Give the user the ability to do global replacements without using regular expressions or user-defined loops.
NOTE:  If the limit is not set, this function acts just as the built-in JavaScript function does.

Parameters:

target The string or regular expression used to find the substring that you would like to be replaced.
NOTE:  If this is a a regular expression, the limit and start parameters will be ignored.
replacement The string that will be replacing the substring which matched target.
[ limit ] The maximum number of replacements which can be carried out.  This parameter is optional and has a default value of 1.  A limit of -1 indicates that a global replacement will occur.
NOTE:  Will be ignored if target is a regular expression.

Returns:
The string with the specified target replaced.  If target is a string, the return value will have it replaced with replacement as many times as was specified by the limit after the specified start.  If target is a regular expression, the return value will have the target substring replaced as many times is specified by the regular expression flags.

Example #1:
The code below will show you how to replace an unlimited amount of occurrences of a substring with another string:

 

Example #2:
The code below will show you how to search for one occurrence of a substring (proving that adding JXtension will not ruin any former code):

 

Example #3:
The code below will show you how to use a regular expression to replace multiple instances of the space character with " " (proving that adding JXtension will not ruin any former code):

 

See also: