Common Operations and special characters of strings in JavaScript, special characters in javascript
The following describes common operations on JavaScript strings. The specific content is as follows:
CharAt () is used to obtain characters at a specific index in a string;
ToupperCase () converts all characters in a string into uppercase letters;
IndexOf () returns the position of the first occurrence of a specified string in the string.
Substring () returns a substring of a string.
Slice () returns a substring in a string that supports the negative number parameter (the first to last character in the string is-1)
Concat () is used to combine multiple strings into one string.
Replace () replaces a substring in a string with a specific string.
Split () Splits a string into multiple strings. You can specify the delimiter.
Match () use a regular expression to search for the target substring
Search () use a regular expression to search for the target substring (index is returned)
Ps: Special JavaScript characters
You can use a backslash in JavaScript to add special characters to a text string.
Insert special characters
A backslash is used to insert ellipsis, line breaks, quotation marks, and other special characters into a text string.
See the following JavaScript code:
var txt="We are the so-called "Vikings" from the north."document.write(txt)
In JavaScript, strings start or end with single or double quotation marks. This means that the above string will be truncated to: We are the so-called.
To solve this problem, you must add a backslash (\) before the quotation marks in "Viking (\). In this way, each double quotation mark can be converted to a literal string.
var txt="We are the so-called \"Vikings\" from the north."document.write(txt)
Now JavaScript can output the correct text string: We are the so-called "Vikings" from the north.
This is another example:
document.write ("You \& me are singing!")
The preceding example generates the following output:
You & me are singing!
The following table lists other special characters that can be added to a text string using a backslash:
Code |
Output |
\' |
Single quotes |
\" |
Double quotation marks |
\& |
And number |
\\ |
Backslash |
\ N |
Line Break |
\ R |
Carriage Return |
\ T |
Tab |
\ B |
Escape Character |
\ F |
Page feed |
Summary
The above is a common operation method for strings in JavaScript described in the editor. I hope it will be helpful to you. If you have any questions, please leave a message. The editor will reply to you in time!