In this example, we will add an anchor for the text:
<SCRIPT type = "text/JavaScript">
VaR TXT = "Hello world! "
Document. Write (txt. Anchor ("myanchor "))
</SCRIPT>
The aboveCodeThe output is pure HTML:
<A name = "myanchor"> Hello world! </A>
4: Blink (), big (), sub (), fixed () and other methods
<HTML>
<Body>
<SCRIPT type = "text/JavaScript">
VaR TXT = "Hello world! "
Document. Write ("<p> big:" + TXT. Big () + "</P> ")
Document. Write ("<p> small:" + TXT. Small () + "</P> ")
Document. Write ("<p> bold:" + TXT. Bold () + "</P> ")
Document. Write ("<p> italic:" + TXT. Italics () + "</P> ")
Document. Write ("<p> Blink:" + TXT. Blink () + "(does not work in IE) </P> ")
Document. Write ("<p> fixed:" + TXT. Fixed () + "</P> ")
Document. Write ("<p> Strike:" + TXT. Strike () + "</P> ")
Document. Write ("<p> fontcolor:" + TXT. fontcolor ("red") + "</P> ")
Document. Write ("<p> fontsize:" + TXT. fontsize (16) + "</P> ")
Document. Write ("<p> lowercase:" + TXT. tolowercase () + "</P> ")
Document. Write ("<p> uppercase:" + TXT. touppercase () + "</P> ")
Document. Write ("<p> Subscript:" + TXT. sub () + "</P> ")
Document. Write ("<p> superscript:" + TXT. Sup () + "</P> ")
Document. Write ("<p> link:" + TXT. Link ("http://www.w3school.com.cn") + "</P> ")
</SCRIPT>
</Body>
</Html>
5: Concat () method
In this example, we will create two strings and then use Concat () to display them as a string:
<SCRIPT type = "text/JavaScript">
VaR str1 = "hello"
VaR str2 = "world! "
Document. Write (str1.concat (str2 ))
</SCRIPT>
The output of the above Code is:
Hello world!
6: indexof () method
NOTE 1: The indexof () method is case sensitive!
NOTE 2: If the string value to be retrieved does not appear, this method returns-1.
In this example, we will! "Different searches within the string:
<SCRIPT type = "text/JavaScript">
VaR STR = "Hello world! "
Document. Write (Str. indexof ("hello") + "<br/> ")
Document. Write (Str. indexof ("world") + "<br/> ")
Document. Write (Str. indexof ("world "))
</SCRIPT>
Output of the above Code:
0
-1
6
7: substring () method
Syntax: stringobject. substring (start, stop): If the stop parameter is omitted, the returned substring ends to the end of the string. If the start parameter is equal to the end parameter, this method returns an empty string (a string with a length of 0 ). If start is greater than end, this method swaps the two parameters before extracting the substring.
Important: Unlike slice () and substr (), substring () does not accept negative parameters.
In this example, we use substring () to extract some characters from the string:
<SCRIPT type = "text/JavaScript">
VaR STR = "Hello world! "
Document. Write (Str. substring (3 ))
</SCRIPT>
Output:
Lo world!
8: Split () method
Syntax: stringobject. Split (Separator,Howmany): ParameterHowmanyYou can specify the maximum length of the returned array. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is not set, the entire string is split, regardless of its length.
Note: If the empty string ("") is usedSeparator, Each character in the stringobject will be split.
Example 1
In this example, we will split the string in different ways:
<SCRIPT type = "text/JavaScript">
VaR STR = "How are you doing today? "
Document. Write (Str. Split ("") + "<br/> ")
Document. Write (Str. Split ("") + "<br/> ")
Document. Write (Str. Split ("", 3 ))
</SCRIPT>
Output:
How, are, you, doing, today?
H, O, W, A, R, E, Y, O, U, D, o, I, n, G, T, O, D,, Y ,?
How, are, you
Example 2
In this example, we split strings with more complex structures:
"2: 3: 4: 5". Split (":") // Returns ["2", "3", "4", "5"]
"| A | B | C". Split ("|") // ["", "A", "B", "C"]
Example 3
Use the following code to split sentences into words:
VaR words = sentence. Split ('')
Or use the regular expression as the separator:
VaR words = sentence. Split (/"s + /)
Example 4
If you want to split words into letters or strings into characters, use the following code:
"Hello". Split ("") // you can return ["H", "E", "L", "L", "O"]
If you only need to return a part of the characters, use the howmany parameter:
"Hello". Split ("", 3) // you can return ["H", "E", "L"]
9: Match () method
The match () method can be used to search for a specified value in a string or to find a match between one or more regular expressions. This method is similar to indexof () and lastindexof (), but it returns the specified value rather than the position of the string.
Syntax: stringobject. Match (searchvalue), where searchvalue is required. Specifies the string value to be retrieved.
Or stringobject. Match (Regexp), where Regexp is required. Specifies the Regexp object of the pattern to be matched. If this parameter is not a Regexp object, you must first pass it to the Regexp constructor and convert it to a Regexp object.
The match () method retrieves the stringobject string to find one or more texts that match Regexp. The behavior of this method depends largely on whether Regexp has a flag.
If Regexp does not mark g, the match () method can only perform one match in stringobject. If no matching text is found, match () returns NULL. Otherwise, it returns an array containing information related to the matched text it finds. The 0th elements in the array store the matching text, while the remaining elements store the text that matches the regular expression's subexpression. In addition to these regular array elements, the returned array also contains two object attributes. The index attribute declares the position of the starting character of the matching text in the stringobject, And the INPUT attribute declares the reference to the stringobject.
If Regexp has a flag, the match () method performs a global search and finds all matching substrings in stringobject. If no matched substring is found, null is returned. If one or more matched substrings are found, an array is returned. However, the content of the array returned by global match is very different from that returned by the former. Its array elements store all matched substrings in stringobject, and there is no index or INPUT attribute.
Note: In global search mode, match () does not provide text information that matches the subexpression, nor declare the position of each matched substring. You can use regexp.exe C () to retrieve global information ().
Example 1
In this example, we will! "For different searches:
<SCRIPT type = "text/JavaScript">
VaR STR = "Hello world! "
Document. Write (Str. Match ("world") + "<br/> ")
Document. Write (Str. Match ("world") + "<br/> ")
Document. Write (Str. Match ("worlld") + "<br/> ")
Document. Write (Str. Match ("world! "))
</SCRIPT>
Output:
World
Null
Null
World!
Example 2
In this example, we use a globally matched regular expression to retrieve all numbers in the string:
<SCRIPT type = "text/JavaScript">
VaR STR = "1 plus 2 equal 3"
Document. Write (Str. Match (/"d +/g)
)
</SCRIPT>
Output:
1, 2, 3