English 中文(简体)
如何将空间从Java号楼铺开?
原标题:How to remove spaces from a string using JavaScript?

如何在扼杀中清除空间? 例如:

<>>Input:

 /var/www/site/Brand new document.docx 

<<>Output>:

 /var/www/site/Brandnewdocument.docx 
最佳回答

这?

str = str.replace(/s/g,   );

var str =  /var/www/site/Brand new document.docx ;

document.write( str.replace(/s/g,   ) );

<>Update: http://stackoverflow.com/questions/5964373/is-there-a-difference-between-s-g-and-s-g> 这个问题:

str = str.replace(/s+/g,   );

这是一个更好的解决办法。 它产生同样的结果,但它更快。

www.un.org/Depts/DGACM/index_spanish.htm Regex

s is the regex for "whitespace", and g is the "global" flag, meaning match ALL s (whitespaces).

here

作为旁观,你可以把你想要的东西的单项引用内容替换为内容,这样你就可以用任何其他扼杀手段取代白色空间。

问题回答

var a = b = " /var/www/site/Brand new   document.docx ";

console.log( a.split(   ).join(  ) );
console.log( b.replace( /s/g,   ) ); 

这样做的两种方式!


基准:

这里我的结果——(2018.07.13) MacOs High Sierra 10.13.3 on Zheng 67.0.3396 (64-bit), Safari 11.0.3 (13604.5.6), 590.0.2 (64-bit) :

SHORT strings

简短说明类似于欧佩斯问题的例子

enter image description here

所有浏览器最快的解决办法是/g(regexp1a)。 - Chrome17.7M(业务/安全),Sharma 10.1M,8.8M。 所有浏览器的速率最小的是split-join 解决方案。 更改<代码>至s 或添加+i至 regexp放慢处理过程。

LONG strings

显示大约3个温和特性的结果是:

  • regexp1a: Safari 50.14 ops/sec, Firefox 18.57, Chrome 8.95
  • regexp2b: Safari 38.39, Firefox 19.45, Chrome 9.26
  • split-join: Firefox 26.41, Safari 23.10, Chrome 7.98,

您可在以下机器上操作:

容易

someString.replace(/ /g,   );
// or
someString.replace(/s/gm,   );

<Without regexp,仅对一次情况进行罚款。

input = input.replace(   ,   );

This is faster as simple ! Could help some of you in some cases.

  var output =  /var/www/site/Brand new document.docx .replace(/ /g, ""); 
    or
  var output =  /var/www/site/Brand new document.docx .replace(/ /gi,"");

注:虽然你在拆除空间时使用g或gi,但两者都是一样的。

如果我们在替代职能中使用g,它将检查确切的配对。 但是,如果我们利用巨头,它忽略了案件的敏感性。

http://www.w3schools.com/jsref/jsref_replace.asp”rel=“nofollow” 。

你们可以利用规章将空间从扼杀中移走。

let str =  /var/www/site/Brand new document.docx ;
let result = str.replace(/s/g,   );

Regex + 替换()

尽管reg鱼可能较慢,但在许多情况下,开发商仅仅在考虑速度不相干时操纵了几条str。 即便是/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/或/

let string =  /var/www/site/Brand new document.docx ;
let path = string.replace(/s/g,   );
// path =>  /var/www/site/Brandnewdocument.docx 

Split() + Join()

利用Split + Join允许对扼杀进行进一步的系统操纵。

let string =  /var/www/site/Brand new document.docx ;
let path => string.split(  ).map(char => /(s|.)/.test(char) ?  /  : char).join(  );
// "/var/www/site/Brand/new/document/docx";

使用<条码> 取代所有似乎最简单。 (我可以为最快者 t)

/var/www/site/Brand new document.docx .replaceAll( , )

:

替代所有(所有)方法回收了一种新的体形,其形形形形形形形形形形形形色色。 模式可以是体力或试射,替代可以是体力,也可以是要求每一方配对的功能。

Easiest way to remove spaces from the string is use replace in this way

let str =  /var/www/site/Brand new document.docx ;
let result = str.replace(/s/g,   );

var str =  /var/www/site/Brand new document.docx ;

document.write( str.replace(/s/g,   ) );


----------




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签