[英]How can I replace all spaces between words in javascript
This should be simple bu I can't get it working. 这应该很简单,但是我无法正常工作。
The title pretty much explains it all. 标题几乎说明了一切。 I tried:
我试过了:
var tags = "best cars ever";
var hashtags = tags.replace("/\s/g", "");
document.write(hashtags);
and also: 并且:
var tags = "best cars ever";
var hashtags = tags.replace("/ /g", "");
document.write(hashtags);
When you use replace()
with a regular expression, don't put that expression inside a string: 当将
replace()
与正则表达式一起使用时,请勿将该表达式放在字符串中:
var tags = "best cars ever"; var hashtags = tags.replace(/\\s/g, ""); console.log(hashtags);
var tags = "best cars ever";
tags.split(" ").join("");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.