最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unescaped '^' with jslint - Stack Overflow

programmeradmin3浏览0评论

This is my code:


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

Why does jslint throw this error?

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

This is my code:


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

Why does jslint throw this error?

Lint at line 5 character 25: Unescaped '^'.
return s.gsub(/[^A-Z^a-z^0-9^\-^_^:^\.]/, ".");
Share Improve this question asked May 3, 2010 at 16:18 BenBen 212 bronze badges 1
  • I like the ^_^! ^_^ (And I wonder if a gsub() function that's effectively just an alias for replace() is really necessary…) – Tomalak Commented May 3, 2010 at 16:38
Add a ment  | 

4 Answers 4

Reset to default 5

Apart from the obvious change to the regex, I remend the following change to the function itself:

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;
}

Don't vote for this...vote for Tomalak's answer if you like this (it is the same as his but without using arguments.callee plus caching the regex itself).

var htmlid = (function(){
    var cache = {},
        reg = /[^A-Za-z0-9_:.-]/;
    return function(s){
        var id = s.replace(reg, ".");
        if (id in cache){ id += cache[id]++;}
        cache[id] = 0;

        return id;
    };
}());

If what you intend to have is a negated character class, then this is what you want:

return s.gsub(/[^A-Za-z0-9_:.-]/, ".");

First of all, thank you for the answers. Your brought a little error to the semantic of the function, as it should return the same id if I query for the same string twice. E.g.:

htmlid("foo bar");  // -> "foo.bar"
htmlid("foo bar");  // -> "foo.bar"
htmlid("foo.bar");  // -> "foo.bar0"
htmlid("foo.bar0"); // -> "foo.bar00"
htmlid("foo.bar");  // -> "foo.bar0"

Howvere, I adopted your functions to:

var htmlid = (function () {
    var cache = {},
        ncache = {},
        reg = /[^A-Za-z0-9_:.-]/;
    return function (s) {
        var id;
        if (s in cache) {
            id = cache[s];
        } else {
            id = s.replace(reg,".");
            if (id in ncache) {
                id += ncache[id]++;
            }
            ncache[id] = 0;
            cache[s] = id;
        }
        return id;
    };
}());
发布评论

评论列表(0)

  1. 暂无评论