[英]Palindrome Checker - can't get the function to check the phrase
My Palindrome checker function works for single strings ie 'dog' it works, but when it is a phrase ie 'nurses run' it does not work! 我的回文检查器功能适用于单个字符串,即“ dog”起作用,但是当它是短语(即“ nurses run”)时不起作用! here is my code:
这是我的代码:
function palindromeCheck(string) {
return string === string.split('').reverse().join('');
}
function palindromeCheck(string) {
string = string.replace(/\s+/g,'');
return string === string.split('').reverse().join('');
}
The s+ character means to match any number of whitespace characters (including tabs). s +字符表示匹配任意数量的空格字符(包括制表符)。 The g character means to repeat the search through the entire string.
g字符表示在整个字符串中重复搜索。 Read about this, and other RegEx modifiers available in JavaScript here .
在此处阅读有关此内容以及JavaScript中可用的其他RegEx修饰符的信息。
Try this. 尝试这个。
function palindromeCheck(string) { string = string.replace(/\\s/g, ""); return string === string.split('').reverse().join(''); } console.log(palindromeCheck('nurses run'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.