javascript - Replacing white spaces to commas - Stack Overflow

Well, i'm using regex to replace all white spaces to mas but there's a white space at the end

Well, i'm using regex to replace all white spaces to mas but there's a white space at the end and i don't know how to make the regex not replace him

What I have

25DEL 38DEL A73G

JS

 var valor = $("#mapaArquivos option:selected").val().replace(/\s+/g, ", ");

Output

25DEL, 38DEL, A73G,

Right Output

25DEL, 38DEL, A73G

Thanks for the help anyway!

Well, i'm using regex to replace all white spaces to mas but there's a white space at the end and i don't know how to make the regex not replace him

What I have

25DEL 38DEL A73G

JS

 var valor = $("#mapaArquivos option:selected").val().replace(/\s+/g, ", ");

Output

25DEL, 38DEL, A73G,

Right Output

25DEL, 38DEL, A73G

Thanks for the help anyway!

Share Improve this question asked Feb 9, 2013 at 4:46 Bruno AndradeBruno Andrade 2613 gold badges8 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Trim the value before applying regex. this way there will be no trailing or leading spaces

var valor = jQuery.trim($("#mapaArquivos option:selected").val())
                  .replace(/\s+/g, ", ");

You could trim the end or you could use

replace( /(?!\s+$)\s+/g, ", " );

A negative look-ahead prevents trailing spaces being replaced.

This should do (it trims the Whitespaces of the initial value first)

var valor = $("#mapaArquivos option:selected").val().replace(/^\s+|\s+$/g,"").replace(/\s+/g, ", ");

if not, you might want to try replace(/\s+$/,"")instead (trims only at the end

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744359814a4570414.html

相关推荐

  • javascript - Replacing white spaces to commas - Stack Overflow

    Well, i'm using regex to replace all white spaces to mas but there's a white space at the end

    7天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信