Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to achieve something very simple and I can't figure out where I'm going wrong.(我正在尝试实现一些非常简单的功能,但我无法弄清楚哪里出了问题。)

See the string below;(参见下面的字符串;)

My Name St Eugene’s Cathedral

I would like it to be;(我希望成为这样;)

my-name-st-eugene-s-cathedral

I need to remove all spaces and single quotes, replace with a hyphen and convert to lowercase.(我需要删除所有空格和单引号,并用连字符替换并转换为小写。)

My code so far is;(到目前为止,我的代码是;)

 var str = "My Name St Eugene's Cathedral" str = str.replace(/'| /g,'-').toLowerCase(); alert(str) 

Link to fiddle here http://jsfiddle.net/8z2jseyr/1/(链接到此处的小提琴http://jsfiddle.net/8z2jseyr/1/)

  ask by jonboy translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
349 views
Welcome To Ask or Share your Answers For Others

1 Answer

It's ' not ' .(这是'' 。)

If you want ' also when making slugs add that one also with another |'(如果您希望'也可以在制造子弹时将其与另一个|'一起添加|')

 var str = "My Name St Eugene's Cathedral" str = str.replace(/'| /g,'-').toLowerCase(); console.log(str) 

for both ' and '(对于'')

 var str = "My Name St Eugene's Cathedral" str = str.replace(/'|'| /g,'-').toLowerCase(); console.log(str) 

With Character Class(有角色类)

 var str = "My Name St Eugene's Cathedral" str = str.replace(/['\s']/g,'-').toLowerCase(); console.log(str) 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...