Repository navigation
Expand file tree
/
Copy pathreplace.html
More file actions
71 lines (69 loc) · 1.87 KB
/
Copy pathreplace.html
File metadata and controls
71 lines (69 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>.replace()</title>
<!-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace -->
<!-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase -->
<!-- http://phpzf.blog.51cto.com/3011675/782012 -->
<!-- https://www.freecodecamp.com/challenges/check-for-palindromes -->
</head>
<body>
<div>
<h1>正则表达式,过滤非数字(0-9),字母(a-z, A-Z)以外的字符!</h1>
</div>
<script>
// reverse str function
function reverseString(str1) {
var a = [];//array
for (var i = 0; i < str1.length; i++) {
a[i] = str1[i];
// console.log("a[i]="+a[i]);
}
a.reverse();
// join()
str1 = a.join('');
return str1;
}
function palindrome(str) {
// Good luck!
var b;
//var reg = /\W/g;
//var reg = /([\(\)\[\]\{\}\^\$\+\,\s\_\:\-\*\?\.\"\'\|\/\\])/g;
var reg = /([\(\)\[\]\{\}\^\$\+\s\_\:\-\*\?\.\"\'\|\/\\])/g;
var s = str.toLowerCase();
//var n = s.filter(reg);//TypeError: s.filter is not a function
var n = s.replace(reg,"");
//You'll need to remove all non-alphanumeric characters
if(n === reverseString(n)){
b = true;
}else{
b = false;
}
//
return b;
}
var xyz;
xyz = palindrome("_eye");
console.log(xyz);
xyz = palindrome("0_0 (: /-\ :) 0-0");
console.log(xyz);
//0000
xyz = palindrome("race car");
console.log(xyz);
//
xyz = palindrome("not a palindrome");
console.log(xyz);
xyz = palindrome("A man, a plan, a canal. Panama");
console.log(xyz);
xyz = palindrome("never odd or even");
console.log(xyz);
xyz = palindrome("My age is 0, 0 si ega ym.");
console.log(xyz);
xyz = palindrome("1 eye for of 1 eye.");
console.log(xyz);
xyz = palindrome("five|\_/|four");
console.log(xyz);
</script>
</body>
</html>