I could not find any usefull post here about my little question.
I have a variable that contains a string and I want it splited into 2 variables.
Example:
var str = "String1;String2";
I want:
var str = "String1;String2";
var string1 = "String1";
var string2 = "String2";
var string1 = str.split(";")[0];
var string2 = str.split(";")[1];
More about split method: Split String Method
You can use the window object for assigning global variables.
var str = "String1;String2";
str.split(';').forEach(function (a) {
window[a] = a;
});
document.write(String1 + ' ' + String2);
Related
I want to replace a floating number by another one in my string using Javascript.
Examples:
var string1 = '$10.50';
var string2 = '$10.50 USD';
var string3 = '10.50 €';
Results:
var newFloatNb = 15.99;
string1 = '$15.99';
string2 = '$15.99 USD';
string3 = '15.99 €';
Anyway to do this? I want to keep the currencies that are not always the same.
I did it, sorry for this question.
var string = '$10.50 USD';
var newFloatNb = 15.99;
var stringNumber = parseFloat(string.match(/-?(?:\d+(?:\.\d*)?|\.\d+)/)[0]).toFixed(2);
string = string.replace(stringNumber, '15.99');
console.log(string);
This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 5 years ago.
Suppose my string is like:
var str = "USA;UK;AUS;NZ"
Now from some a source I am getting one value like:
country.data = "AUS"
Now in this case I want to remove "AUS" from my string.
Can anyone please suggest how to achieve this.
Here is what I have tried:
var someStr= str.substring(0, str.indexOf(country.data))
In this case I got the same result.
var str = "USA;UK;AUS;NZ"
console.log(str + " <- INPUT");
str = str.split(';');
for (let i = 0; i < str.length; i++) {
if (str[i] == 'AUS') {
str.splice(i, 1);
}
}
console.log(str.join(';') + " <- OUTPUT");
You can use split and filter:
var str = "USA;UK;AUS;NZ"
var toBeRemoved = "AUS";
var res = str.split(';').filter(s => s !== toBeRemoved).join(';');
console.log(res);
Try this :
var result = str.replace(country.data + ';','');
Thanks to comments, this should work more efficently :
var tmp = str.replace(country.data ,'');
var result = tmp.replace(';;' ,';');
You can use replace() with a regex containing the searched country, this is how should be the regex /(AUS;?)/.
This is how should be your code:
var str = "USA;UK;AUS;NZ";
var country = "AUS";
var reg = new RegExp("("+country+";?)");
str = str.replace(reg, '');
console.log(str);
This will remove the ; after your country if it exists.
Here is a good old split/join method:
var str = "USA;UK;AUS;NZ;AUS";
var str2 = "AUS";
var str3 = str2 + ";";
console.log(str.split(str3).join("").split(str2).join(""));
I have three variables in javascript like
var a = document.getElementById("txtOrderNumberRelease1").value;
var b = document.getElementById("txtOrderNumberRelease2").value
var c = document.getElementById("ddlOrderNumberRelease3");
and
var mainString ="ROAM-LCD-Synergy-789-456-LLX WARRANTY"
In mainString, the value "789" is coming from variable "a",
value "456" is coming from variable b and value "LLX" is coming from variable "c".
Variables "a" and "b" will always be Integers, whereas variable "c" will always be one the three values ie. "LLI,LLA,LLX".
Before value "789", there can be any number of words splitted by hypen "-". like ROAM-LCD-Synergy-SSI etc...
But after the value of variable "c" i.e "LLX" in mainString, there can be only one word for eg. "WARRANTY".
Now my issue is, I have to replace these three values of "a","b" and "c" ie. "789-456-LLX" with my newly entered values lets say 987-654-LLA, then my desired final string would be
old string: mainString ="ROAM-LCD-Synergy-789-456-LLX WARRANTY"
desired string: mainString ="ROAM-LCD-Synergy-987-654-LLA WARRANTY"
Please suggest some wayout.
Thanks
var a = 123;
var b = 456;
var c = "ABC";
var mainString ="ROAM-LCD-Synergy-789-456-LLX WARRANTY";
var updatedString = mainString .replace(/\d{3}-\d{3}-[A-Z]{3}\s([^\s]*)$/,a+"-"+b+"-"+c+" $1");
console.log(updatedString);
Something like this should work:
mainString.replace(/[\d]{3}\-[\d]{3}\-[\w]{3}\s([\w]+)$/, a + "-" + b + "-" + c + " $1");
Here's an option that uses string.split.
function replaceLastThreeDashedFields(original,a,b,c) {
var spaceSplit = original.split(' ');
var dashSplit = spaceSplit[0].split('-');
dashSplit[dashSplit.length - 3] = a;
dashSplit[dashSplit.length - 2] = b;
dashSplit[dashSplit.length - 1] = c;
var newDashed = dashSplit.join('-');
spaceSplit[0] = newDashed;
var newSpaced = spaceSplit.join(' ');
return newSpaced;
}
Just change the values before they are assigned? Place this before your variables are declared.
document.getElementById("txtOrderNumberRelease1").value = '987';
document.getElementById("txtOrderNumberRelease2").value = '654';
document.getElementById("txtOrderNumberRelease3").value = 'LLA';
The following code should update based on the values of a, b, and c...
var mainString ="ROAM-LCD-Synergy-789-456-LLX WARRANTY";
var a = document.getElementById("txtOrderNumberRelease1").value;
var b = document.getElementById("txtOrderNumberRelease2").value;
var c = document.getElementById("ddlOrderNumberRelease3");
mainString = mainString.replace("789",a);
mainString = mainString.replace("456",b);
mainString = mainString.replace("LLX",c);
I have some strings like:
str1 = "Point[A,B]"
str2 = "Segment[A,B]"
str3 = "Circle[C,D]"
str4 = "Point[Q,L]"
Now I want to have function that gives me character after "[" and the character before "]". How could I make something like that ?
try this one...
var str = "Point[A,B]";
var start_pos = str.indexOf('[') + 1;
var end_pos = str.indexOf(']',start_pos);
var text_to_get = str.substring(start_pos,end_pos)
alert(text_to_get);
You'd need regex to do that
var matches = /\[(.*?)\]/.exec(str1);
alert(matches[1]);
You can use match() to extract the characters:
str.match(/\[(.*)\]/)[1]
A safer way would be:
var matches = str.match(/\[(.*)\]/);
if(matches) {
var chars = matches[1];
}
Here's an approach which avoids regex.
var str = "Point[A,B]";
var afterOpenBracket = str.split("[")[1]; // returns "A,B]"
var bracketContents = afterOpenBracket.split("]")[0]; // returns "A,B"
There, pretty simple! bracketContents now contains the entirety of the text between the first set of brackets.
We can stop here, but I'll go a step further anyway and split up the parameters.
var parameters = bracketContents.split(","); // returns ["A", "B"]
Or in case u have more [A,C,D,B] and don't want to use regex:
var str1 = "Point[A,C,D,B]";
function extract(str1){
var a = str1.charAt(str1.indexOf('[')+1);
var b = str1.charAt(str1.indexOf(']')-1);
return [a, b];
//or
//a.concat(b); //to get a string with that values
}
console.log(extract(str1));
How to split a string in JavaScript with the "," as seperator?
var splitString = yourstring.split(',');
See split
var str = "test,test1,test2";
var arrStr = str.split(',');
var arrLength = arrStr.length; //returns 3
Use split to split your string:
"foo,bar,baz".split(",") // returns ["foo","bar","baz"]
var expression = "h,e,l,l,o";
var tokens = expression.split("\,");
alert(tokens[0]);// will return h