Using JavaScript replace () method

Working with strings in JavaScript is performed in the format of string object methods, but allows the use of the functionality of others, for example, arrays, numbers, and user-defined descriptions.

javascript replace

The JavaScript replace method is of particular importance; it is often difficult to replace with other combinations.

Syntax and Application Example

The double and single apostrophe characters are used to describe strings: “” ”and“ '”. To write a regular expression, the slash“ / ”is accepted. But you should not forget that a pair of“ // ”characters is not a record of an empty regular expression, but a comment.

javascript text replace

In this example, the JavaScript 'text' replace construct works with just one character: 'a' and 'A'. Expression (1) does not use any additional hints (search options), therefore the method only changes the first occurrence of the letter 'a'. The following two expressions show that the meaning of using the search option i is case-sensitive search of the character (string). Applying the g option - allows you to replace all occurrences of the desired character (string).

Improved Application Example

From the point of view of perception and understanding, the given JavaScript replace example does not work properly: it is difficult to visually detect a completed replacement. If you apply this method to “replace the changes made” with a color change tag, then good work will be done and the perception of the function will improve significantly.

javascript text replace line break

The original row has been changed. After forming a string of an example of using the JavaScript replace method, the string 'dst' is obtained. The replace method is also applied to it in order to get the string 'dst_replace', in which the desired entry (indicated by a green circle) and all replacements will be indicated in blue and a different size.

The replacement string uses the characters '$ &' which mean "insert found occurrence". This is a very convenient feature. In fact, the developer gets the opportunity to write a regular expression, the application of which can be tracked when forming the result string.

Using search result

Using JavaScript replace, the developer can get (as a result of executing a regular expression):

  • $ & - desired;
  • $ `- that goes to the sought;
  • $ 'is what comes after what is sought.

Of the above, it is advisable to use only the first, although an experienced developer (or the task requires it) can use all the options. It is also possible to use the construction of $ n and $ nn, which means referring to the nth match of the regular expression. But it is advisable to avoid using this feature.

javascript text replace

By common logic, the JavaScript replace method is aimed at improving the convenience of working with strings and implements a flexible string processing mechanism. Linking to a specific occurrence limits the flexibility of the algorithm, and choosing what comes before the match or after it can create a cumbersome result.

Function in JavaScript replace construct

Instead of the second parameter of the method, you can use a function whose operation result will be substituted for the detected match.

javascript text replace not working

In this example, if the regular expression matches, the scChange () function is called, which determines the number of parameters received and generates output.

javascript replace

Here, the function does not do anything significant, but demonstrates the content of the parameters it receives. After each JavaScript replace call, the line break "<br/>" is used as a delimiter. The result is approximately the following:

javascript text replace

It can be seen that each time three parameters are passed to the function:

  • the first is the required substring;
  • the second is the position of its entry;
  • the third is the original string.

Depending on the regular expression that the function receives, the data will vary significantly in both quantity and content.

Features of the application of the method

JavaScript replace is a tool for flexible string handling. Using a function as the second parameter makes this tool not only flexible, but also surprisingly diverse.

javascript replace not working

In fact, the browser language provides the developer with the opportunity to formulate a regular expression as a condition for making a decision that performs the function. There are no special restrictions on the function; the ability to detect the number of passed parameters is available by definition: this is the classic JavaScript functionality.

The difficulty is regular expressions themselves. It is necessary to be extremely careful when compiling them and not to strive with one expression to solve the problem. It is often more convenient to disassemble the required string in stages. It is simpler, more reliable and gives a controlled algorithm and a transparent result.

Source: https://habr.com/ru/post/K8809/


All Articles