Introduction to JavaScript string matching
In JavaScript, which we have discussed in previous articles, there are some default methods for creating more complex client-side Web applications. Among these methods, match() is one of the built-in methods used to retrieve data results in the format of string data types, as well as regular expressions in JavaScript. A regular expression is one of those objects, and if you assume that a regular expression (RegExp) object is not in this format, that is, a non-regular expression object, it can also be automatically and implicitly converted to a regular expression with the help of the new keyword, which is used to create the object. If sometimes string and array formats return null, these objects will also return an array as the return value.
grammar
JavaScript built-in methods also have their own features and uses in client-side scripting; In web-based applications, it is more complex. Match () is also one of the predefined methods, and we’ll see the syntax for these methods.
< HTML > <body> <script> function functionname() {var variablename= "; Var v1 = variablename. Match (" strings "); ----some javascript logics----- } </script> </body> </html>
The code above is the basic syntax for the match() method in JavaScript; Basically, the method passes regular expressions as parameters that match the given string.
How does JavaScript string matching work?
In comparison, JavaScript strings will be used for regular expressions, because strings are nothing more than a set of concatenated characters called strings. In regular expressions, it is an expression that defines a sequence of characters for a search pattern. Patterns are just strings, so pattern search is also called string search; It is also used in algorithms like find, find, and replace, which are two important operations for users to validate strings.
If we suppose we want to know if we have found a pattern that matches the string data type to get more information using the test() or search() methods, we can use the exec() or match() methods, but they are slower to execute. If we use these methods and find it, they return an array of data types and update their properties if the string is successfully matched. Properties related to regular expression objects also use RegExp. If the matching value fails, it uses the exec() method and returns a null value.
If we do not access the values of the array types in the regular expression, we do not need to access their properties. Another way is to create an array in a separate variable script. Two different statements occur using some kind of regular expression object, so there is a different set of values for using their index attributes; If the properties we need to access the regular expression are already created by objects that use them, and it has already been initialized, we should assign it as a variable. An array has some content that depends on the presence or absence of global values (flags), or sometimes it can pass null values. If the string is assumed to find no matching set in the script, the global value is used, and the result of the match, using the full set of regular expressions, is returned, displaying the result as a matching string.
The global value is not set or matched, otherwise only the first set of string values will be matched, and it has been returned with respect to the group. These returned values have their additional attributes. These objects have their own attributes, which can be named groups, whose groups are matched by keys that are names, or sometimes we use ids that have integer types, while values are captured using groups or undefined values. With indexes, we have to search for groups and values in the script.
Examples of Javascript string matching
Different examples are mentioned below.
Patients with a
code
<! DOCTYPE html> <html> <body> <p>Welcome To My Domain</p> <button onclick="sam()">Please click it</button> <p id="demo"></p> <script> function sam() { var s = "Welcome To My Domain,Once again the user has welcomed"; var result = s.match(/in/g); document.getElementById("demo").innerHTML = result; } </script> </body> </html>
The output
Example 2
code
<! DOCTYPE html> <html> <body> <p>Welcome To My Domain</p> <button onclick="first()">Please click it</button> <p id="demo"></p> <script> function first() { var s1 = "Welcome To My Domain, Once again the user has welcomed"; var result = s1.match(/in/gi); document.getElementById("demo").innerHTML = result; } </script> </body> </html>
The output
Example 3
code
<! DOCTYPE html> <html> <body> <p>Welcome To My Domain</p> <p id="demo"></p> <FORM ID="first" NAME="first" METHOD=POST ACTION="javascript:void(0)"> <P>RegularExpression: <INPUT TYPE=TEXT NAME="n1" VALUE="\bt[a-z]+\b" SIZE=30></P> <P>User String is: <INPUT TYPE=TEXT NAME="n2" VALUE="The javascript regular Expression Object" SIZE=30></P> <P><INPUT TYPE=SUBMIT VALUE="Matched" ONCLICK="ex()"> <INPUT TYPE=SUBMIT VALUE="Value Matched" ONCLICK="ex2()"></P> <P>String Replaced: <INPUT TYPE=TEXT NAME="n3" VALUE="values replaced" SIZE=30></P> <P>The Given Output is: <INPUT TYPE=TEXT NAME="n4" VALUE="The final result is" SIZE=30></P> <P><INPUT TYPE=SUBMIT VALUE="value replaced" ONCLICK="ex3()"></P> </FORM> <script> function ex() { var r = new RegExp(document.first.n1.value); if (document.first.n2.value.match(r)) { alert("Values Successfully matched"); } else { alert("No matching values found"); } } function ex2() { var r = new RegExp(document.first.n1.value); var ma = r.exec(document.first.n2.value); if (ma == null) { alert("No matching values found"); } else { var s1 = "matching values found " + ma.index + ":\n"; for (i = 0; i < ma.length; i++) { s1 = s1 + ma[i] + "\n"; } alert(s1); } } function ex3() { var r = new RegExp(document.first.n3.value, "g"); document.first.n4.value = document.first.n2.value.replace(r, document.first.n3.value); } </script> </body> </html>
The output
conclusion
In JavaScript, regular expressions also copy strings from one place to another; The string of the regular expression does not include the global value set (flags), so the str.match() method finds and will return the same result as the regexp.exec () method. We can also use regexp.test () to determine if a string is matched, and it supports browser compatibility.