简体   繁体   English

使用JavaScript删除字符串

[英]Removing string using javascript

I have a string like 我有一个像

 var test="ALL,l,1,2,3";

How to remove ALL from string if it contains using javascript. 如果使用JavaScript,如何从字符串中删除ALL。

Regards, 问候,

Raj 拉吉

you can use js replace() function: 您可以使用js replace()函数:

http://www.w3schools.com/jsref/jsref_replace.asp http://www.w3schools.com/jsref/jsref_replace.asp

so: 所以:

test.replace("ALL,", "");

If the word All can appear anywhere or more than once (eg "l,1,ALL,2,3,ALL") then have such code: 如果单词All可以出现在任何地方或不止一次(例如“ l,1,ALL,2,3,ALL”),则应具有以下代码:

var test = "l,1,ALL,2,3,ALL"
var parts = test.split(",");
var clean = [];
for (var i = 0; i < parts.length; i++) {
   var part = parts[i];
   if (part !== "ALL")
      clean.push(part);
}
var newTest = clean.join(",");

After this the variable newTest will hold the string without ALL . 之后,变量newTest将保存不带ALL的字符串。

如果您要做的就是从另一个字符串中删除出现的字符串“ ALL”,则可以使用JavaScript String对象的replace方法

test.replace("ALL","");

I'm not really sure if you want to remove all instances of capital letters from your string, but you are probably looking at using a regular expression such as s.replace(/[AZ]/g,"") where s is the string. 我不确定是否要从字符串中删除所有大写字母的实例,但是您可能正在使用正则表达式,例如s.replace(/ [AZ] / g,“”),其中s是串。
Looking up javascript RegExp will give more indepth details. 查找javascript RegExp将提供更多详细信息。

采用:

test.replace ( 'ALL,',  '' ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM