Strings and Regular expressions (RegExp)
I recommend reading this article: talk about Unicode encoding and briefly explain UCS, UTF, BMP, BOM, etc
Some new methods
- codePointAt()
- String.fromCodePoint()
- normalize()
Regular expressionumark
- When switching to the U flag, the pattern of the Unicode code is used for matching.
- When switching to the Y flag, using the “sticky” search, the match starts at the current position of the target string, and the Y flag can be used.
Method to identify if a string exists in another string
- includes()Method if the given text exists anywhere in the string
true
Otherwise returnfalse
. - startsWith()Method that returns when the given text appears at the beginning of the string
true
Otherwise returnfalse
. - endsWith()Method that returns when the given text appears at the end of the string
true
Otherwise returnfalse
.
Each of the above methods takes two parameters: the text to search for as the first parameter, and an optional index of the search starting position. When the second argument is provided, the includes() and startsWith() methods try to match from that index position; The endsWith() method, on the other hand, subtracts this parameter from the length of the string to start trying to match. When no second argument is provided, the includes() and startsWith() methods start at the beginning of the string, and the endsWith() methods start at the end.
const str = "Hello world !"
console.log(str.includes('world')) // true
console.log(str.startsWith('Hello')) // true
console.log(str.endsWith('! ')) // true
console.log(str.includes('a')) // false
console.log(str.startsWith('! ')) // false
console.log(str.endsWith('Hello')) // false
console.log(str.startsWith("o".4)) // true
console.log(str.endsWith("o".8)) // true
console.log(str.includes("o".8)) // false
Copy the code
Repeat () method
The repeat() method, which takes an argument as the number of repeats of the string, returns a new string that repeats the initial string a specified number of times. The following
console.log('x'.repeat(4)) // xxxx
console.log('hello'.repeat(4)) // Hello hello hello hello hello
console.log('zzzhim'.repeat(4)) // zzzhimzzzhimzzzhimzzzhim
Copy the code
Flags properties
ES6 has added the Flags attribute to work with the source attribute to make it easier to get flags. Both of these properties are prototype accessor properties that only have getters, so they are read-only.
const reg = /ab/g
console.log(reg.source) // ab
console.log(reg.flags) // g
Copy the code
Template literals
Template literals are string literals that allow expressions to be embedded. Allows us to do multi-line strings and string interpolation by using template literals.
The basic grammar
Template literals use backquotes (‘) instead of double and single quotes in regular strings. Template strings can be inserted with (‘ ${} ‘) to insert variables, expressions, and so on.
const a = 'world'
const str = `Hello ${a}! `
const str1 = ` 1 + 2 =The ${1 + 2}`
console.log(str) // Hello world!
console.log(str1) // 1 + 2 = 3
Copy the code
Tagging template
A template tag converts template literals and returns the final string value. The tag is specified at the beginning of the template, before the first ‘.
function tag(literals, ... substitutions) {
let str = ' '
let len = literals.length > substitutions.length ? literals.length : substitutions.length
// Returns a string
for(let i = 0; i < len; i++) {
str += literals[i] ? literals[i] : ' '
str += substitutions[i] ? substitutions[i] : ' '
}
return str
}
const str = 'world'
let message = tag`Hello ${str}! youThe ${'good'}Ah! `
console.log(message) // "Hello world! Hello!"
Copy the code
conclusion
ES6 adds full Unicode support, as well as new ways to manipulate strings, and regular expressions also introduce a lot of functionality. The addition of template literals makes it easier to concatenate and manipulate strings and variables. And with templates we can create functions that take snippets of template literals as arguments and use them to return appropriate strings.
6 Reading notes.
- ES6 block-level binding, let, const declaration
- Strings and Regular expressions (RegExp)
- Functions
- Extended object functionality
- Deconstruction assignment
- Symbol and Symbol attribute
Refer to the article
- MDN
- Understanding ECMAScript 6
- Understanding ECMAScript 6