开发者

Problems with creating regex in JavaScript

I'm working on regex (regexes, actually) that will satisfy my needs. :)

I need to replace given string (named title) and return slug:

  1. Replace one white-space with one underscore in places where are one white-space. If there are more then one white-space, only one white-space should be replaced. It's hard to explain, but I will try to explain with example. Hi and hello, world! would be Hi_and_hello,_world!, but if there would be, for example, two white-spaces before 'world', it would be Hi_and_hello,_ world!,

  2. Replace all remaining white-spaces with nothing (''),

  3. Replace all 开发者_运维知识库unwanted characters (white-list: a-z, A-Z, 0-9 and underscore). In other words, if symbol isn't in the white-list, it should be replaced with nothing (''),

  4. Trim beginning and end from underscores;

The end result should be:

Hello, world! I'm known as daGrevis. :)

...to:

Hello_world_Im_known_as_daGrevis

All that stuff I need to do in the JavaScript. This is what I got so far:

slug = title.replace(/\s+/g, '_');
slug = title.replace(/\s+/g, '');
slug = title.replace(/[^\w0-9a-zA-Z]/g, '');

I'm not good with regexies so don't laugh on me. :D Thanks in an advice!


In other words:

  1. Replace all consecutive spaces by one underscore
  2. Remove all unwanted characters
  3. Remove the underscores at the beginning and end

There you go:

var slug = title.replace(/\s+/g, '_');
slug = slug.replace(/[^0-9a-z_]/gi, '');
slug = slug.replace(/^_+|_+$/g, "");
  • \s+ matches as much consecutive whitespace (space, newline, tab, ..) as possible
  • /g is a global flag, means: "select every match"
  • /[^a-z0-9_]/gi means: Anything which is not an alphanumeric character or underscore, case insensitive
  • ^ and $ are markers for the beginning and end of a string.
    ^_+|_+$/g means: match every underscore at the beginning and end of a string

Don't forget to perform the following replacements on slug, instead of title. Otherwise, you "forget" your previous replacements.


Here's how I'd do it:

slug = title.replace(/[^\s\w]+/g, '').replace(/\s+/g, '_').replace(/^_|_$/g, '');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜