如果给定的字符串是回文,则返回 true
;否则返回 false
。
使用 toLowerCase()
转换字符串,并使用 replace()
从中删除非字母数字字符。 然后,在将其转换为 tolowerCase()
之后,将 split('')
为单独的字符,reverse()
,join('')
并与原始非反转字符串进行比较。
const palindrome = str => { const s = str.toLowerCase().replace(/[\W_]/g, ''); return ( s === s .split('') .reverse() .join('') ); };
palindrome('taco cat'); // true
更多代码 JavaScript 实用代码片段 请查看 https://www.7psus5.com/30-seconds-of-code/
注:回文就是一句话正着读和反着读一模一样。
最新评论
写的挺好的
有没有兴趣翻译 impatient js? https://exploringjs.com/impatient-js/index.html
Flexbox playground is so great!
感谢总结。
awesome!
这个好像很早就看到类似的文章了
比其他的教程好太多了
柯理化讲的好模糊…没懂