Str.replace: effective examples

The str.replace method complements the capabilities of strings. For each line, you can determine which part of it must be replaced with a different sequence of characters.

A regular expression can act as a replacement string, replacement is performed for all identical occurrences, and the original string does not change.

Traditional use of replace method

The syntax for str.replace is simple: a string, a replaced expression, a replacement string. The source string and replacement string can contain any characters within the JavaScript syntax, and the replaced expression is either a regular string or a regular expression.

javascript str replace

A regular expression can be defined directly in a method or using a RegExp object. The absence of specific β€œregular” elements in the string, which is replaced by the replacement string, does not play a role.

str replace js

It is important to consider the first parameter of the method as a regular expression on the simple basis that JavaScript does this, and this avoids unexpected errors.

Functional Application of the Method

In a browser language, a developer can use a function in almost every method. JavaScript str.replace is no exception. Any function is called at the time of the match in the source string of the search substring. The called function receives information about what exactly and in what position it is detected.

The function can be universal and serve various lines, since the original line is also passed to it.

str replace js

The result that scFuncReplace returns is substituted for the found occurrence. If the replacement string is "-" then the overall result makes obvious sense; if you remove the "-" and uncomment the string "*! *", The effect will not be as expected.

It also makes sense not to forget that the usual way to designate a pattern that is searched for the purpose of replacing - β€œ/” in the form of β€œ//” is interpreted as a comment and everything that follows this pair of characters does not matter in most cases. However, this does not interfere with the calculation of the pattern string in the expression, although it is doubtful that a search with a replacement by "//" matters.

Semantics and lowercase information processing

The ability to call a function in the str.replace method is an effective solution in the context of information processing, rather than replacing occurrences of one string with another. Just replacing matters in simple algorithms. For example, you can fix input errors for a visitor to a page:

  • commas instead of dots;
  • Russian letters "o" in words in Latin;
  • really erroneous characters;
  • extra characters, etc.

Strings in JavaScript have a semantic load and can carry situational or contextual meaning. In the first case, we can talk about how to form the result on the source line, that is, the function through the return statement makes changes to the source line and produces the result.

In the second case, the source line controls the formation of the result in general, that is, in a completely different context, in a different information environment.

Lowercase semantics

The source line may well be a semantically loaded sentence, and the developer is instructed to develop an algorithm in which the source sentence is the goal, and the result of processing the sentence is the answer to the source information.

If we ignore characters and focus on strings as a sentence (remember that words and phrases are included in the sentence structure), the use of str.replace rises to a qualitatively higher level.

Recursive semantics in string processing

The problem is that when using str.replace js and regular expressions, the unit of attention is a single character, not a word. It is impossible to formulate a β€œregular” for working with words, phrases and sentences - not on that scale. One can manage strings as sequences of characters in minimally significant cases.

Almost all real tasks are focused on the use of semantically significant information, that is, from the level of elementary symbols it is necessary to go to the level of words - as symbols, phrases - as systems of words, but also symbols in the context of sentences.

Complex semantics

A recursive solution to the problem, in which semantically meaningful information units are represented in the format of individual characters and fall into the "competence" of regular expressions in the context of str.replace, allows you to develop expert and effective information processing algorithms.

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


All Articles