gpt4 book ai didi

javascript - 使用 jslint 未转义 '^'

转载 作者:行者123 更新时间:2023-12-03 10:05:27 25 4
gpt4 key购买 nike

这是我的代码:

/********************************************************** * remove non-standard characters to give a valid html id * **********************************************************/function htmlid(s) { return s.gsub(/[^A-Z^a-z^0-9^\-^_^:^\.]/, ".");}

为什么 jslint 会抛出这个错误?

Lint at line 5 character 25: Unescaped '^'.return s.gsub(/[^A-Z^a-z^0-9^\-^_^:^\.]/, ".");

最佳答案

除了对正则表达式的明显更改外,我建议对函数本身进行以下更改:

function htmlid(s) {
// prevents duplicate IDs by remembering all IDs created (+ a counter)
var self = arguments.callee;
if (!self.cache) self.cache = {};

var id = s.replace(/[^A-Za-z0-9_:.-]/, "."); // note the dash is at the end!
if (id in self.cache) id += self.cache[id]++;
self.cache[id] = 0;

return id;
}

关于javascript - 使用 jslint 未转义 '^',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2759487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com