charCodeAt--returns a number indicating the ISO-Latin-1 codeset value of the character at the given index.
concat--combines the text of two strings and returns a new string.
fromCharCode--constructs a string from the specified sequence of numbers that are ISO-Latin-1 codeset values.
match--used to match a regular expression against a string
replace--used to find a match in a string, and replace the matched substring with a replacement substring.
search--used to test for a match in a string.
slice--extracts a section of an string and returns a new string.
split*--uses a regular expression or a fixed string as its argument to split a string.
substr--returns the characters in a string collecting the specified number of characters beginning with a specified location in the string.
substring*--When the <SCRIPT> tag includes LANGUAGE="JavaScript1.2", substring(x,y) no longer swaps x and y.
string.charCodeAt([index])
string is any string.
index, an optional argument, is any integer from 0 to string.length-1, or a property of an existing object. The default value is 0.
String object
"ABC".charCodeAt(0)
string1.concat(string2)
string1 is the first string.
string2 is the second string.
String object
concat combines the text from two strings and returns a new string. Changes to the text in one string do not affect the other string.
<SCRIPT LANGUAGE="JavaScript1.2">This writes: The morning is upon us. The sun is bright.
str1="The morning is upon us. "
str2="The sun is bright."
str3=str1.concat(str2)
document.write(str3)
</SCRIPT>
String.fromCharCode(num1, num2, ..., numn)
numn is a sequence of numbers that are ISO-Latin-1 codeset values.
String object
String object.
String.fromCharCode(65,66,67)Example 2. : The
which property of the KeyDown, KeyPress, and KeyUp events contains the ASCII value of the key pressed at the time the event occurred. If you want to get the actual letter, number, or symbol of the key, you can use fromCharCode. The following example returns the letter, number, or symbol of the KeyPress event's which property.
String.fromCharCode(KeyPress.which)
string.match(regexp)
string is any string.
regexp is the name of the regular expression. It can be a variable name or a literal.
String object
g (for global) and i (for ignore case) flags in the regular expression. These can be included separately or together. The following two examples below show how to use these flags with match.
NOTE: If you are executing a match simply to findtrueorfalse, usesearchor the regular expressiontestmethod.
match is used to find 'Chapter' followed by one or more numeric characters followed by a decimal point and numeric character zero or more times. The regular expression includes the i flag so that case will be ignored.
<SCRIPT LANGUAGE="JavaScript1.2">This returns the array containing
str = "For more information, see Chapter 3.4.5.1";
re = /(chapter \d+(\.\d)*)/i;
found = str.match(re);
document.write(found);
</SCRIPT >
Chapter 3.4.5.1,Chapter 3.4.5.1,.1
'Chapter 3.4.5.1' is the first match and the first value remembered from (Chapter \d+(\.\d)*). '.1' is the second value remembered from (\.\d).
Example 2. :
The following example demonstrates the use of the global and ignore case flags with match.
<SCRIPT LANGUAGE="JavaScript1.2">The returned array contains D, d.
str = "abcDdcba";
newArray = str.match(/d/gi);
document.write(newArray);
</SCRIPT >
string.replace(regexp, newSubStr)
string is a string. This string can include the RegExp properties $1,..., $9, lastMatch, lastParen, leftContext, and rightContext.
regexp is the name of the regular expression. It can be a variable name or a literal.
newSubStr is the string to replace the string found with regexp.
String object
g (for global) and i (for ignore case) flags in the regular expression. These can be included separately or together. The following two examples below show how to use these flags with replace.
replace to replace each occurrence of 'apples' in the string with 'oranges.'
<SCRIPT LANGUAGE="JavaScript1.2">This prints "oranges are round, and oranges are juicy." Example 2.: In the following example, the regular expression is defined in
re = /apples/gi;
str = "Apples are round, and apples are juicy.";
newstr=str.replace(re, "oranges");
document.write(newstr)
</SCRIPT>
replace and includes the ignore case flag.
<SCRIPT LANGUAGE="JavaScript1.2">This prints "Twas the night before Christmas..." Example 3. : The following script switches the words in the string. For the replacement text, the script uses the values of the
str = "Twas the night before Xmas...";
newstr=str.replace(/xmas/i, "Christmas");
document.write(newstr)
</SCRIPT>
$1 and $2 properties.
<SCRIPT LANGUAGE="JavaScript1.2">This prints "Smith, John".
re = /(\w+)\s(\w+)/;
str = "John Smith";
newstr = str.replace(re, "$2, $1");
document.write(newstr)
</SCRIPT>
string.search(regexp)
string is any string.
regexp is the name of the regular expression. It can be a variable name or a literal.
search returns the index of the regular expression inside the string. Otherwise, it returns -1.
When you want to know whether a pattern is found in a string use search (similar to the regular expression test method); for more information (but slower execution) use match (similar to the regular expression exec method).
function testinput(re, str){
if (str.search(re) != -1)
midstring = " contains ";
else
midstring = " does not contain ";
document.write (str + midstring + re.source);
}
string.slice(beginslice,[endSlice])
string is a string.
beginSlice is the zero-based index at which to begin extraction.
endSlice is the zero-based index at which to end extraction.
String object
slice extracts the text from one string and returns a new string. Changes to the text in one string do not affect the other string.
<SCRIPT LANGUAGE="JavaScript1.2">This writes: morning is upon
str1="The morning is upon us. "
str2=str1.slice(3,-5)
document.write(str2)
</SCRIPT>
split has the following additions:
string.split([separator], [limit])
string is any string.
separator specifies the character or regular expression to use for separating the string.
limit is an optional integer that specifies a limit on the number of splits to be found.
String object
separator is removed from the string and the substrings are returned in an array. If separator is omitted, the array contains one element consisting of the entire string.
If separator is a regular expression, any included parenthesis cause submatches to be included in the returned array.
Using the optional limit argument, you can avoid including trailing empty elements in the returned array.
LANGUAGE="JavaScript1.2", the following script produces
["She", "sells", "seashells", "by", "the", "seashore"]
<SCRIPT LANGUAGE="JavaScript1.2">Without
str="She sells seashells \nby the\n seashore"
document.write(str + "<BR>")
a=str.split(" ")
document.write(a)
</SCRIPT>
LANGUAGE="JavaScript1.2", the above script splits only on single space characters, producing
She,sells,,,,seashells, by,,,the ,seashoreExample 2.: In the following example,
split looks for zero to many spaces followed by a semi-colon followed by zero to many spaces and, when found, removes them from the string. nameList is the array returned as a result of split.
<SCRIPT LANGUAGE="JavaScript1.2">This prints two lines; the first line prints the original string, and the second line prints the resulting array. Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ["Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand"]
names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
document.write (names + "<BR>" + "<BR>");
re = /\s*;\s*/;
nameList = names.split (re);
document.write(nameList);
</SCRIPT>
str.substr(start, [length])
str is any string.
start is the location at which to begin extracting characters.
length, an optional argument, is the number of characters to extract.
String object
start is a character index. The index of the first character is 0, and the index of the last character is str.length-1. substr begins extracting characters at start and collects length number of characters.
If start is positive and is longer than str.length-1, substr returns no characters.
If start is negative, substr uses it as a character index from the end of the string. If start is negative and abs(start) is larger than the length of the string, substr uses 0 is the start index.
If length is 0 or negative, substr returns no characters. If length is omitted, start extracts characters to the end of the string.
<SCRIPT LANGUAGE="JavaScript1.2">
str = "abcdefghij"
document.writeln("(1,2): ", str.substr(1,2))
document.writeln("(-2,2): ", str.substr(-2,2))
document.writeln("(1): ", str.substr(1))
document.writeln("(-20, 2): ", str.substr(1,20))
document.writeln("(20, 2): ", str.substr(20,2))
</SCRIPT>This script displays:
(1,2): bc
(-2,2): ij
(1): bcdefghij
(-20, 2): bcdefghij
(20, 2):
LANGUAGE="JavaScript1.2" in the script tag, substring(x,y) no longer swaps x and y.
string.substring(indexA, [indexB])
string is any string.
indexA is any integer from zero to stringName.length-1.
indexB, an optional argument, is any integer from zero to stringName.length.
String object
substring behaves as follows:
indexA up to but not including indexB.
indexA is less than zero, indexA is treated as if it were 0.
indexB is greater than stringName.length, indexB is treated as if it were stringName.length.
indexA equals indexB, substring returns an empty string.
indexB is omitted, indexA extracts characters to the end of the string.
LANGUAGE="JavaScript1.2" in the <SCRIPT> tag,
Without LANGUAGE="JavaScript1.2",
LANGUAGE="JavaScript1.2", the following script produces a runtime error (out of memory).
<SCRIPT LANGUAGE="JavaScript1.2">Without
str="Netscape"
document.write(str.substring(0,3);
document.write(str.substring(3,0);
</SCRIPT>
LANGUAGE="JavaScript1.2", the above script prints
Net Net
In the second write, the index numbers are swapped.
Last Updated: 10/22/97 11:48:06