Common Operations and special characters of strings in JavaScript, special characters in javascript

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.