Home > Web Front-end > JS Tutorial > Common operation methods and special characters of strings in JavaScript

Common operation methods and special characters of strings in JavaScript

亚连
Release: 2018-05-29 15:39:41
Original
1658 people have browsed it

This article mainly introduces the common operation methods of strings in JavaScript and js special characters. Friends in need can refer to

Let’s take a look at the common operation methods of js strings. The specific content is as follows :

Charat () to get the characters in the specific index in the string; The position where a specific string appears for the first time in the string

substring() Returns a substring of the string

slice() Returns a substring of the string, supports negative parameters ( The last character in the string is set to -1)

concat()                                                                                                                                                                                                can be used to concatenate multiple strings into one string

replace()                                                               Replace substrings with specific strings

split()                                                                       ’ s ’ s ’ s ’ s ’ s ’ s to be 1 to 1 s .            String

search() Use regular expression to search the target substring (the index is returned)

ps: JavaScript special characters

You can use backslashes in JavaScript to add special characters to text strings. Insert special characters

Backslashes are used to insert ellipses, newlines, quotation marks, and other special characters in text strings.

Please look at the following JavaScript code:

var txt="We are the so-called "Vikings" from the north."
document.write(txt)
Copy after login
In JavaScript, strings use single quotes or double quotes to start or end. 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". This converts each double quote into a literal string.

var txt="We are the so-called \"Vikings\" from the north."
document.write(txt)
Copy after login

Now JavaScript can output the correct text string: We are the so-called "Vikings" from the north.

Here is another example:

document.write ("You \& me are singing!")
Copy after login

The above example will produce the following output:

You & me are singing!

The table below lists The remaining special characters can be added to the text string using backslashes:

Code Output
\' Single quotes
\" Double quotes
\& and ampersand
\\ Backslash
\n Line break
\r Carriage return character
\t Tab character
\b Backspace character
\f Form feed character

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to use EL expressions to obtain context parameter values ​​in JS

jQuery realizes text exceeding 1 line, 2 How to automatically add ellipsis when a line or a specified number of lines is used

The problem and solution of {{}} flickering when vue renders

The above is the detailed content of Common operation methods and special characters of strings in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template