将单词之间的空格转换为+符号的最佳方法是什么?

时间:2017-11-17 07:53:17

标签: javascript regex

该函数应将输入值转换为查询:

1. "   bald   valves  " to "bald+valves"
2. " buffers some  " to "buffers+some"

这是我目前的解决方案:

query.trim().replace(/\s\s+/g, ' ').replace(/[\s.;,?%]/, '+')
你能帮我改进吗?

1 个答案:

答案 0 :(得分:0)

怎么样

s.trim().replace(/\s+/g,'+')

"   bald   valves  ".trim().replace(/\s+/g,'+')
"bald+valves"
" buffers some  ".trim().replace(/\s+/g,'+')
"buffers+some"