how to convert this string to float?
i want result 900.50
case-1: var convertThis = 'any string here 900,50 also any string here';
case-2: var convertThis = 'any string here 900.50 also any string here';
How to do this?
Try following code:
var text = 'any string here 900,50 also any string here';
var matched = text.match(/\d+[,.]\d+/)[0].replace(',', '.');
var num = parseFloat(matched, 10);
console.log(matched);
console.log(num);
prints:
900.50
900.5
You could do this :
var num = parseFloat(convertThis.replace(/[^\d\.,]/g,'').replace(/,/,'.'));
But be aware that this would break as soon as you have more than one number or dot in your text. If you want something reliable, you need to be more precise about what the string can be.
Supposing you'd want to extract all numbers from a more complex strings, you could do
var numbers = convertThis.split(/\s/).map(function(s){
return parseFloat(s.replace(',','.'))
}).filter(function(v) { return v });
Here, you'd get [900.5]
var myFloat = +(convertThis.match(/\d+[,\.]?\d+/)[0].replace(",","."))
Related
I've been trying to get something like this to work but it just won't:
var str = str.slice(0, -"variable");
I need to remove a variable amount of characters from the end. thanks!
Use slice:
var str = "Hello, World";
var chars = 7;
var sliced = str.slice(0, -chars);
console.log(sliced);
This -"variable" is a coercion logic, so basically you're executing something as follow: str.slice(0, NaN);
console.log(-"variable"); // js engine is trying to convert to number
Just use the variable, you don't need to wrap it with quotes like a string.
var variable = 5;
var str = "EleFromStack".slice(0, -variable);
console.log(str)
I have a string like "home/back/step" new string must be like "home/back".
In other words, I have to remove the last word with '/'. Initial string always has a different length, but the format is the same "word1/word2/word3/word4/word5...."
var x = "home/back/step";
var splitted = x.split("/");
splitted.pop();
var str = splitted.join("/");
console.log(str);
Take the string and split using ("/"), then remove the last element of array and re-join with ("/")
Use substr and remove everything after the last /
let str = "home/back/step";
let result = str.substr(0, str.lastIndexOf("/"));
console.log(result);
You could use arrays to remove the last word
const text = 'home/back/step';
const removeLastWord = s =>{
let a = s.split('/');
a.pop();
return a.join('/');
}
console.log(removeLastWord(text));
Seems I got a solution
var s = "your/string/fft";
var withoutLastChunk = s.slice(0, s.lastIndexOf("/"));
console.log(withoutLastChunk)
You can turn a string in javascript into an array of values using the split() function. (pass it the value you want to split on)
var inputString = 'home/back/step'
var arrayOfValues = inputString.split('/');
Once you have an array, you can remove the final value using pop()
arrayOfValues.pop()
You can convert an array back to a string with the join function (pass it the character to place in between your values)
return arrayOfValues.join('/')
The final function would look like:
function cutString(inputString) {
var arrayOfValues = inputString.split('/')
arrayOfValues.pop()
return arrayOfValues.join('/')
}
console.log(cutString('home/back/step'))
You can split the string on the '/', remove the last element with pop() and then join again the elements with '/'.
Something like:
str.split('/');
str.pop();
str.join('/');
Where str is the variable with your text.
How to convert a number with prefix into double/float e.g. STA01.02to 1.02?
Use regex to strip the non-numbers (excluding ".") for a more flexible solution:
parseFloat("STA01.02".replace(/[^0-9\.]+/g, ''));
// Assumed "STA0" is the fixed-length prefix, you can adjust the substring at the start you're getting rid of.
var myString = "STA01.02";
var noPrefix = myString.substring(4); // Just "1.02"
var myNumber = parseFloat(noPrefix);
console.log(myNumber); // Prints 1.02
If the prefix always the same...
var str = "STA01.02";
var number = parseFloat(str.substring(3));
I have an string like'[[br,1,4,12],[f,3]]'. I want to split as strings and integers and put it into array like the string [['br',1,4,12],[f,3]].string maybe like '[]' or '[[cl,2]]',ect...but the words only,br,cl,fand i. How does get the array. Any idea for this problem?
Thanks
You can do conversion that you wanted by using RegEx :
Get your string
var str = '[[br,1,4,12],[f,3]]';
str = str.replace(/([a-zA-Z]+)/g, '"$1"');
console.log(str);
//Outputs :
[["brd",1,4,12],["f",3]] // It is still just a string
If you wanted to convert it to object, you might use this :
var str = '[[br,1,4,12],[f,3]]';
function toJSObject(str){
str = str.replace(/([a-zA-Z]+)/g, '"$1"');
return (JSON.parse(str))
}
var obj = toJSObject(str);
I have this function :
string = string.replace(/^.*?([a-zA-Z])/, '$1');
and I'd like to save both strings : the one after the expression and the one removed.
How can I do it?
<script type="text/javacript">
var str = '44234lol';
var parts = str.split(/([a-zA-Z]+)/);
alert(parts[0]);
alert(parts[1]);
</script>
This would show what was removed from the original string and what you're left with (I've altered your regex but you could use the same technique) -
var portionremoved;
var string = '1234GF'
string = string.replace(/(\d+)([A-Z]+)/,function (removed,first,second) {
portionremoved = first;
return second;
});
alert(portionremoved);
alert(string);
string1 = string.replace(/^.*?([a-zA-Z])/, '$1');
Note that string.replace returns the replaced string while string still holds the previous value.