SoFunction
Updated on 2025-03-10

Uses of exec, test, match, search, replace, split in js

exec: Perform regular processing of string and return the matching result.array[0] is the original string, and array[i] is the position of the match in the entire searched string.
test: Test whether string contains matching results, returns true if included, and does not include return false if included.
match(pattern): perform regular matching based on pattern. If it matches, the matching result will be returned. If the match fails, return null
search(pattern): perform regular matching based on pattern. If a result is matched, it returns its index number; otherwise -1 is returned
replace(pattern,replacement): perform regular matching based on pattern, replace the matching result with replacement
split(pattern): perform regular segmentation based on pattern, returning a divided array
Example:
Copy the codeThe code is as follows:

function checkForm() {
var u = document.form_name.;
var s = /^[-a-zA-Z0-9_]+(\.[-a-zA-Z0-9_]+)*@[-a-zA-Z0-9_]+(\.[-a-zA-Z0-9_]+)*$/;
var a = (u);
var a = (u);
var a = (s);
var a = (s);
alert(a);
}


replace

The simplest ability is to calculate it is simple character replacement. The sample code is as follows:
Copy the codeThe code is as follows:

var u = "javascript is a good script language";
// Here I want to replace the letter a with the letter A
var s = /a/g;
var a = (s,"A");
</script>

split
Copy the codeThe code is as follows:

<script type="text/javascript">
var str="How are you doing today?"
((" ") + "<br />")
(("") + "<br />")
((" ",3))
</script>

The output result is:
How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you