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);
Related
For example we have next string in javaScript
var str = "abc,de 55,5gggggg,hhhhhh 666 "
How i can get 55,5 as number?
It depends on what you can assume about your number, but for the example and a lot of cases this should work:
var str = "abc,de 55,5gggggg,hhhhhh";
var match = /\d+(,\d+)?/.exec(str);
var number;
if (match) {
number = Number(match[0].replace(',', '.'));
console.log(number);
} else console.log("didnt find anything.");
var str = "abc,de 55,5gggggg,hhhhhh"
var intRegex = /\d+((.|,)\d+)?/
var number = str.match(intRegex);
console.log(number[0]);
JS fiddle:
https://jsfiddle.net/jiteshsojitra/5vk1mxxw/
var str = "abc,de 55,5gggggg,hhhhhh"
str = str.replace(/([a-zA-Z ])/g, "").replace(/,\s*$/, "");
if (str.match(/,/g).length > 1) // if there's more than one comma
str = str.replace(',', '');
alert (str);
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);
I have a String "SHELF-2-1-1-2-1", I need to remove "2" from that string and want the output to be "SHELF-1-1-2-1"
I tried:
var str = "SHELF-2-1-1-2-1";
var res = str.split("-");
How can I join the array to get "SHELF-1-1-2-1"?
This would work:
var str = "SHELF-2-1-1".split('-2').join('');
Sounds like you want to do a replace... Try:
var res = str.replace('-2', '');
var str = "SHELF-2-1-1";
var res = str.split("-");
res.pop(res.indexOf('2'));
var newStr = res.join('-');
This should also work for your updated question, as it will only remove the first 2 from the string
let str = "Hello India";
let split_str = str.split("");
console.log(split_str);
let join_arr = split_str.join("");
console.log(join_arr);
string sChar = "_$$$ASDF 123-456-789123123XXX";
string sChar = "$$VIC123-456-789pppEEX";
I would like to parse the above examples of sChar to result in the following value
123-456-789
What this regex would do is find the first Number in the string as well as the next 10 characters. The next 10 characters can be special characters, alpha, or numberic.
Here the solution for you:
var sChar = "_$$$ASDF 123-456-789123123XXX";
//string sChar = "$$VIC123-456-789pppEEX";
var indexDigit = sChar.search(/[\d]/);
var str = sChar.substring(indexDigit, indexDigit+11);
alert(str);
I see an answer like this:
var str = sChar.match(/\d.{10}/);
alert(str)
That won't work:
Try the following:
var sChar = "_$$$ASDF 123-4$6-7";
var sChar2 = "$$VIC987-6$4-3";
var indexDigit = sChar.search(/[\d]/);
var str = sChar.substring(indexDigit, indexDigit+11);
alert(str);//returns "123-4$6-7"
var str2 = sChar2.match(/\d.{10}/);
alert(str2);//returns null
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));