find substring
includes , startWith
Both methods support a second parameter n
, which means to search for a substring from the beginning of the current 第n个位置
string; the default is 0
.
includes()
: Returns a boolean value indicating whether the parameter string was foundstartWith()
: Returns a boolean value indicating whether the parameter string is at the head of the current stringendWith
endsWith(str,n)
: Returns a boolean value, indicating whether the stringstr
is at the end of the current string,n
indicating that the search range is the current string前n个字符
, then
default is the current stringlength
repeat
repeat(n)
method that repeats the current stringn
times and returns a新字符串
- If n is a string, it will be automatically converted to a number
- If n is a decimal, it will be automatically rounded
- n cannot be a
负数(-1~0之间的小数除外)
sumInfinity
, otherwise an error will be reported n
NaN
is equivalent to0
pathStart , pathEnd
padStart(len,str)
,padEnd(len,str)
, these two methods are used to complete the string,len
specifying the length of the completed string;str
it is the string used to complete, the default is a space- If the length of the original string is greater than or equal
len
to, return the original string If the sum of the original string and the
str
length is greater thanlen
the length, truncatestr
the extra parttemplate string
template string,
反引号``
used to identify
Use as normal string
console.log(typeof `haha`); // string
Used to define multi-line strings
Whitespace and indentation are preserved when outputting
let w = `haha
hehe
heiehi`;
console.log(w);
/*
haha
hehe
heiehi */
backticks to be escaped
Use backticks in template strings \
to escape them with a backslash, for example:`\`hello\` World!`
Embedded variables
The variable name should be written in ${}
, {}
and can be placed in any js表达式
, can be operated, can refer to object properties, and can call functions
{}
The variables in must be declared, otherwise an error will be reported{}
If the value in is not a string, it will be converted to a string according to the general ruleslet x = 1; let y = 2; let obj = { a: 3, b: 4 }; let arr = [1, 'hi']; let fn = function () { console.log('hello'); } console.log(`${x}` + `${y}`); // 12 console.log(`${obj.a + 1 + obj.b}`);// 8 console.log(`${arr}`); // 1,hi `${fn()}` // hello
nested
``
javascript const tmpl = addrs =>
${addrs.map(addr =><tr><td>${addr.first}</td></tr> <tr><td>${addr.last}</td></tr>
).join('')}
`;
const data = [
{ first: '
*/
### 标签模板 即`` fn`${}` ``的形式,`fn`是一个函数,`fn`被调用来处理后面的模板字符串 - 函数`fn`的第一个参数是一个数组,包含模板字符串中没有被变量替换的部分,其他参数是模板字符串中各个被变量替换后的值
javascript
let a = 5;
let b = 'hello';
fnze
; // [["ze"]]
fnone${a}two${b}${'three'}
; // [["one", "two", "", ""], 5, "hello", "three"]
function fn(arr) { console.log(arguments); }
```