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
you can use js replace() function:
http://www.w3schools.com/jsref/jsref_replace.asp
so:
test.replace("ALL,", "");
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.
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","");
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.
use:
test.replace ( 'ALL,', '' );
Related
How to replace each occurrence of a string pattern in a string by another string?
var text = "azertyazerty";
_.replace(text,"az","qu")
return quertyazerty
You can also do
var text = "azertyazerty";
var result = _.replace(text, /az/g, "qu");
you have to use the RegExp with global option offered by lodash.
so just use
var text = "azertyazerty";
_.replace(text,new RegExp("az","g"),"qu")
to return quertyquerty
I love lodash, but this is probably one of the few things that is easier without it.
var str = str.split(searchStr).join(replaceStr)
As a utility function with some error checking:
var replaceAll = function (str, search, replacement) {
var newStr = ''
if (_.isString(str)) { // maybe add a lodash test? Will not handle numbers now.
newStr = str.split(search).join(replacement)
}
return newStr
}
For completeness, if you do really really want to use lodash, then to actually replace the text, assign the result to the variable.
var text = 'find me find me find me'
text = _.replace(text,new RegExp('find','g'),'replace')
References:
How to replace all occurrences of a string in JavaScript?
Vanilla JS is fully capable of doing what you need without the help of lodash.
const text = "azertyazerty"
text.replace(new RegExp("az", "g"), "qu")
I have a string something like this:
http://stackoverflow.com/questions/ask
And would like to return this part:
http://stackoverflow.com/questions/
How can I do this using pure javascript?
Thanks!
This will match and remove the last part of a string after the slash.
url = "http://stackoverflow.com/questions/ask"
base = url.replace(/[^/]*$/, "")
document.write(base)
Help from: http://www.regexr.com/
For slicing off last part:
var test = 'http://stackoverflow.com/questions/ask';
var last = test.lastIndexOf('/');
var result = test.substr(0, last+1);
document.write(result);
You can accomplish this with the .replace() method on String objects.
For example:
//Regex way
var x = "http://stackoverflow.com/questions/ask";
x = x.replace(/ask/, "");
//String way
x = x.replace('ask', "");
//x is now equal to the string "http://stackoverflow.com/questions/"
The replace method takes two parameters. The first is what to replace, which can either be a string or regex, literal or variable, and the second parameter is what to replace it with.
im trying to remove all text except one in javascript
the idea is do something like
//jQuery("someelement").attr("class");
var classes= "cnode_1 timestamp_1413504000 item";
classes = classes.replace(/^(?!timestamp_)/,'');
i want to take only the text who starts with timestamp_, the expected ouput is :
timestamp_1413504000
i want this , then to grab the number
//now "classes" show be timestamp_1413504000
classes = classes.replace("timestamp_","");
the expected ouput is :
1413504000
i want to avoid use something like, split the clasess base on space, then use for bucle, and finally compare with indexOf
Do you need the "timestamp_" for something?
Why not just
= classes.replace(/^.*timestamp_(\d+).*/img, "$1");
Just use .match to get the part you want.
var classes= "cnode_1 timestamp_1413504000 item";
// match will return the matched string and the groups in an array
var ts = classes.match(/timestamp_(\d+)/)[1]; // match[1] is the group (\d+)
// 1413504000
I know you said that you want to avoid splitting or using indexOf, but javaScript doesn't have a 'startsWith' function. Why do you want to avoid doing that? Is the following unacceptable?
var NumberOutput;
var classList = document.getElementById('elementYouAreLookgingAt').className.split(/\s+/);
for (var i = 0; i < classList.length; i++) {
if (classList[i].indexOf("someClass") == 0) {
NumberOutput = classList[i].replace("someClass", "");
}
}
I have a string like
/abc/def/hij/lmn.o // just a raw string for example dont know what would be the content
I want only /abc/def/hij part of string how do I do that.
I tried using .split() but did not get any solution.
If you want to remove the particular string /lmn.o, you can use replace function, like this
console.log(data.replace("/lmn.o", ""));
# /abc/def/hij
If you want to remove the last part after the /, you can do this
console.log("/" + data.split("/").slice(1, -1).join("/"));
# /abc/def/hij
you can do
var str = "/abc/def/hij/lmn.o";
var dirname = str.replace(/\/[^/]+$/, "");
Alternatively:
var dirname = str.split("/").slice(0, -1).join("/");
See the benchmarks
Using javascript
var x = '/abc/def/hij/lmn.o';
var y = x.substring(0,x.lastIndexOf("/"));
console.log(y);
var s= "/abc/def/hij/lmn.o"
var arr= s.split("/");
after this, use
arr.pop();
to remove the last content of the array which would be lmn.o, after which you can use
var new_s= arr.join("/");
to get /abc/def/hij
I have the following string: (17.591257793993833, 78.88544082641602) in Javascript
How do I use split() for the above string so that I can get the numbers separately.
This is what I have tried (I know its wrong)
var location= "(17.591257793993833, 78.88544082641602)";
var sep= location.split("("" "," "")");
document.getElementById("TextBox1").value= sep[1];
document.getElementById("Textbox2").value=sep[2];
Suggestions please
Use regular expression, something as simple as following would work:
// returns and array with two elements: [17.591257793993833, 78.88544082641602]
"(17.591257793993833, 78.88544082641602)".match(/(\d+\.\d+)/g)
You could user Regular Expression. That would help you a lot. Together with the match function.
A possible Regexp for you might be:
/\d+.\d+/g
For more information you can start with wiki: http://en.wikipedia.org/wiki/Regular_expression
Use the regex [0-9]+\.[0-9]+. You can try the regex here.
In javascript you could do
var str = "(17.591257793993833, 78.88544082641602)";
str.match(/(\d+\.\d+)/g);
Check it.
If you want the values as numbers, i.e. typeof x == "number", you would have to use a regular expression to get the numbers out and then convert those Strings into Numbers, i.e.
var numsStrings = location.match(/(\d+.\d+)/g),
numbers = [],
i, len = numsStrings.length;
for (i = 0; i < len; i++) {
numbers.push(+numsStrings[i]);
}