Saturday, July 29, 2017

JavaScript applications

 JavaScript applications

One of the most common scripting languages ??today is JavaScript, which is often used to allow access to objects from other applications.

Typically, this is used to develop dynamic websites. This language was designed as a Java-like look, but because of its user-friendly interface, even those who are not programming experts can deal with this language.

A bit of history first: JavaScript started as Mocha, which is created by Brendan Eich who worked for Netscape. Later, Mocha was changed to LiveScript, but finally Netscape added support for Java in its browser, reason why it is renamed to JavaScript. However, it is not related to the Java programming language despite its name.

When it comes to replacing texts, it mainly uses the ".replace" method.

This allows the substitution of texts within strings with the use of regular expressions. Here is a useful example:

For example, it has this variable:

Var foo = "How do I replace text within a string in JavaScript?";

Now, if you are going to replace the "replace" with "modify", you can do this:

Var bar = foo.replace (/ replace /, 'modify');

The result will look like this:

How do I modify text within a string in JavaScript?

In the first parameter, you will notice that replace () is known as a regular expression. If you are replacing a certain text, you have to make sure that it is limited by using a slash (/) and you should check that it is not included in quotation marks.

An example of an incorrect method to replace is as follows:

Foo.replace ('/ replace /,' modify ');

Notice that the reason why it is wrong is because of the quotes. On the other hand, because the replace () considered as a regular expression, you can add other things to it. This includes ^ at the beginning to match the opening of the string. Another option is to add $ in the last part of the string to match.

Here's an example of this is to match a selection of letters, which is a a e e in this case:

Var foo = "that that";

Var bar = foo.replace (/ ^ that /, 'this');

Now, what happens here is that this will be the new variable bar in your script:

What that

It also has another option that is to replace a series of HTML texts that are included in a certain element. Here, you can use jQUery ie the jQuery html () function when you get and set the HTML code.

As an example, you can take a look at this:

<P id = "something"> </ p>
How do I replace text within a string in JavaScript?

To transform the text, you can use this:

$ ("#something"). Html ($ ("# something") .Html (). Replace (/ replace /, 'modify'));

This may seem a little complicated for beginners, but this is a perfect practice for them to improve their programming skills. Now, you can replace the texts with your desired replacement in Javascript with no problem at all.

0 comments:

Post a Comment