admin管理员组文章数量:1329040
I'm trying to split a string in two parts with the separators "T" & "L".
T49.80000305175781L60.00000190734863
So, I'm expecting an array with these elements:
["49.80000305175781","60.00000190734863"]
I have tried these but they don't work.
var xPos = xPos.split('T', 'L'); //Returns empty
var xPos = xPos.split(/T/, /L/); //Returns empty
var xPos = xPos.split(/T/); //Returns: ["", "49.80000305175781L60.00000190734863"]
What should be the best approach?
I'm trying to split a string in two parts with the separators "T" & "L".
T49.80000305175781L60.00000190734863
So, I'm expecting an array with these elements:
["49.80000305175781","60.00000190734863"]
I have tried these but they don't work.
var xPos = xPos.split('T', 'L'); //Returns empty
var xPos = xPos.split(/T/, /L/); //Returns empty
var xPos = xPos.split(/T/); //Returns: ["", "49.80000305175781L60.00000190734863"]
What should be the best approach?
Share Improve this question edited Sep 1, 2017 at 11:00 dda 6,2132 gold badges27 silver badges35 bronze badges asked May 31, 2016 at 7:02 Björn CBjörn C 4,00811 gold badges51 silver badges87 bronze badges 1- Possible duplicate of how to find a number in a string using javascript – Mihir Kale Commented May 31, 2016 at 7:07
6 Answers
Reset to default 12You could use a regular expression with a positive lookahead.
console.log('T49.80000305175781L60.00000190734863'.split(/(?=[TL])/));
Or without the letters, just a split where the letters appears.
console.log('T49.80000305175781L60.00000190734863'.split(/[TL]/));
Just for pleteness, a proposal with String.match in a single step
console.log('T49.80000305175781L60.00000190734863'.match(/[^TL]+/g));
You can use .match()
with RegExp
/\d+\.\d+/g
console.log("T49.80000305175781L60.00000190734863".match(/\d+\.\d+/g))
Split takes two optional arguments. First one is the delimiter while the second one is the limit. You can find here the documentation.
If you want to split it with multiple delimiters you should use a regex:
console.log('T49.80000305175781L60.00000190734863'.split(/T|L/));
I don't think you can use split with multiple separators like that.
What you can do is use a regular expression to split the string.
xPos.split(/T|L/g)
should work.
Ninas answer is great! I really mean it. But just to make it interesting if you would have more of these numbers/arrays or what ever you want to call it you could use a similar way like Nina, but use regular expression for letters to get all the values without the need to declare all of the letters.
With the letters included:
console.log('T49.80000305175781L60.00000190734863C34.534623460304040003'.split(/(?=[a-zA-Z])/));
Without the letters:
console.log('T49.80000305175781L60.00000190734863C34.534623460304040003'.split(/[a-zA-Z]/));
Just replace the character with blank character and store that string into new variable. You will get all the number
本文标签: splitHow to find numbers in the following string using JavaScriptStack Overflow
版权声明:本文标题:split - How to find numbers in the following string using JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742224036a2435652.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论