最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

split - How to find numbers in the following string using JavaScript? - Stack Overflow

programmeradmin6浏览0评论

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
Add a ment  | 

6 Answers 6

Reset to default 12

You 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

发布评论

评论列表(0)

  1. 暂无评论