-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
hyphenate.html
48 lines (44 loc) · 1.36 KB
/
hyphenate.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
/*
* var aFn = cached(function(string){
*
* return string
* })
* aFn(string1);
* aFn(string2);
* aFn(string);
* aFn(string1);
* aFn(string2);
*
* aFn 函数会多次调用 里面就能体现了
* 用对象去缓存记录函数
* */
function cached(fn) {
var cache = Object.create(null);
return (function cachedFn(str) {
var hit = cache[str];
return hit || (cache[str] = fn(str))
})
}
var hyphenateRE = /\B([A-Z])/g;
// * \B的用法
// \B是非单词分界符,即可以查出是否包含某个字,如“北斗狼神”中是否包含“狼”这个字。
// \b单词边界,如果字符的左右两边有空白字符则为单词边界
// \B'非单词边界'字符左右两边没有空白字符
//就是匹配
var hyphenate = cached(function (str) {
return str.replace(hyphenateRE, '-$1').toLowerCase();
});
console.log(hyphenate('ddAbc '))
console.log(hyphenate('DdAbc '))
console.log(hyphenate('Dd Abc '))
</script>
</body>
</html>