JavaScript分割字符串但保留分割字符在数组中。

huangapple go评论57阅读模式
英文:

javascript split a string but keep the split character in the array

问题

以下是翻译好的部分:

我有以下两种类型的纬度/经度字符串:

2154S15002E

215422N1500232E

我希望在N或S上拆分字符串,以下方法可以工作,但它会删除N或S,因此我得到一个rp数组,内容为2154 15002E,而我想要的是2154S 15002E。

var rp = rPoint.split(/\s*(?:N|S)\s*/);

如何拆分并保留'S'?还是有比拆分更好的方法吗?

谢谢。

英文:

I have the following 2 types of lat/long strings

2154S15002E

215422N1500232E

I wish to split the string on the N or the S, the following works but it drops the N or S so I get an rp array of 2154 15002E, when i want 2154S 15002E

var rp = rPoint.split(/\s*(?:N|S)\s*/);

How do I split and keep the ‘S’? Or is there a better way than split?

Thank you

答案1

得分: 0

你可以只匹配数字和字母的模式。

console.log('2154S15002E'.match(/\d+[SE]/g));
英文:

You could just match the pattern of digits and letter.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(&#39;2154S15002E&#39;.match(/\d+[SE]/g));

<!-- end snippet -->

答案2

得分: 0

var rp = rPoint.split(/(?<=N|S)/);

英文:

Used lookbehind assertions to get split after N or S

var rp = rPoint.split(/(?&lt;=[NS])/);

huangapple
  • 本文由 发表于 2023年3月7日 04:57:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定