Generally, this is utilized to produce dynamic internet sites. This language was made as a look a like of Java but mainly because of its pleasant interface, even all those who are not qualified in programming can deal with this language.
When it will come to changing texts, it mostly uses the “.replace” method.
This allows the substitute of texts within strings with the use of typical expressions. In this article is a valuable instance:
For instance, you have this variable:
Now, if you are going to substitute the “substitute” with “modify,” you can do this:
var bar = foo.replace (/swap/, ‘modify’)
The result will look like this:
In the to start with parameter, you will discover that the swap () is acknowledged as a frequent expression. if you are likely to exchange a certain text, you have to make guaranteed that it is bounded applying a slash (/) and you must test that you have not enclosed them in rates.
An illustration of a mistaken process to substitute is the following:
foo.exchange (‘/swap/, ‘modify’)
Recognize that the cause why it is improper is due to the fact of the prices. On the other hand, due to the fact the swap() viewed as as a normal expression, you can increase other stuffs in it. This features the ^ at the commencing to match the opening of the string. One more option is to incorporate $ at the past aspect of the string so that it will match.
Here is an instance of this is to match a collection of letters, which is a to e in this case:
var foo = “that that that that”
var bar = foo.swap (/^that/, ‘this’)
Now, what occurs below is that this will be the new variable bar in your script:
this that that that
You also have a different choice which is to replace a selection of HTML texts that are integrated in a specific ingredient. Below, you can use jQUery namely the function html () from jQuery when you get and established the HTML.
As an case in point, you can just take a appear at this:
To remodel the text, you can use this:
$ (“#a thing”).html ($(“#a little something”).html().exchange (/exchange/, ‘modify’))