1

I have a string like

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

How to remove ALL from string if it contains using javascript.

Regards,

Raj

rajputhch
  • 627
  • 4
  • 22
  • 36

5 Answers5

2

you can use js replace() function:

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

so:

test.replace("ALL,", "");
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • how about providing an example? It wouldn't take long and make your answer much more useful. – meouw Mar 14 '11 at 15:38
1

If the word All can appear anywhere or more than once (e.g. "l,1,ALL,2,3,ALL") then have such code:

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.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • Use strict equality. `if (part !== "ALL")` – Matt Ball Mar 14 '11 at 16:05
  • @Matt what are the advantages of this, if I may ask? – Shadow The GPT Wizard Mar 14 '11 at 16:08
  • The [strict equality operator](https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators) (`===`) does not perform type coercion if the operand types are different, is generally "good practice", and is a teeny bit faster. See [this question]( http://stackoverflow.com/questions/359494/) for more. – Matt Ball Mar 14 '11 at 16:32
  • 1
    @Matt cheers, I'm convinced. For those reading the comments, the most simple example is `alert(5 == "5")` vs. `alert(5 === "5")` - the first returns true as the type doesn't matter so JS is converting types by itself while the second will show false as the values are of different types. In this case `split()` returns array of strings so the type is guaranteed to be the same. :) – Shadow The GPT Wizard Mar 15 '11 at 08:08
0

If all you want to do is remove occurrences of the string "ALL" from another string, you can use the JavaScript String object's replace method:

test.replace("ALL","");
Greg
  • 33,450
  • 15
  • 93
  • 100
0

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(/[A-Z]/g,"") where s is the string.
Looking up javascript RegExp will give more indepth details.

Ant
  • 43
  • 2
  • 8
0

use:

test.replace ( 'ALL,',  '' ); 
bensiu
  • 24,660
  • 56
  • 77
  • 117