[英]delete specific character from a string in js
I have a string that returns from google map api. 我有一个从Google Map API返回的字符串。 it's about the latitude and longitude, that's how it looks
(25.229355, 55.302734)
它是关于纬度和经度的样子
(25.229355, 55.302734)
I want to remove (
, )
, and the space
in order to use these two numbers. 我想删除
(
, )
和space
以使用这两个数字。
I want these two numbers in an array for example or alert each one of them. 例如,我希望将这两个数字排列在一个数组中,或者提醒它们中的每一个。
Parse with a regular expression. 用正则表达式解析。 This is the simplest case:
这是最简单的情况:
var input = "(25.229355, 55.302734)";
var arr = input.match(/[0-9\.]+/g); alert(arr);
var lat = arr[0];
var long = arr[1];
str = "(25.229355, 55.302734)";
str =str.replace("(","");
str =str.replace(")","");
str =str.replace(",","");
var n=str.split(" ");
console.log(n);
Alright, usually if you are trying to extract things from a string, you need to figure out: 好吧,通常,如果您尝试从字符串中提取内容,则需要弄清楚:
In your case, what you don't want is parentheses and spaces. 对于您而言,不需要的是括号和空格。 In JS, there is a function you can call on strings called replace(substr,replacement) which will do exactly what it sounds like.
在JS中,有一个函数可以调用称为replace(substr,replacement)的字符串,该函数将完全像听起来那样。
Lets assume your paired string is called latLong. 假设您的配对字符串称为latLong。 Once you've removed what you don't want:
删除不想要的内容后:
latLong.replace(/[()\s]/g, "");
then you can split up the string based on the delimiter between them (the comma) using the split() method 然后您可以使用split()方法根据它们之间的定界符(逗号)分割字符串
var latLongArray = latLong.split(",");
The split() method will divide a string up into an array of substrings, separated by the argument you pass it. split()方法会将字符串分成多个子字符串数组,并由传递的参数分隔。 So at the end of this, your latLongArray will be an array containing 2 elements (the latitude and longitude)!
因此,在此结束时,您的latLongArray将是一个包含2个元素(纬度和经度)的数组!
try it 试试吧
var x= '(25.229355, 55.302734)'.replace(/[^A-Za-z\s]+/g, '');
// x is : `25.229355 55.302734`
var n=x.split(" ");
alert("Long is" + n[0] + " lat is "+ n[1] )
You keep on saying it's an object, in which case it makes no sense to parse the string. 您一直在说这是一个对象,在这种情况下,解析字符串是没有意义的。 If you really need a string output, just get the numbers directly from the object and concatenate it.
如果确实需要输出字符串,则直接从对象中获取数字并将其连接起来。
obj[0]+","+obj[1]
How about... 怎么样...
var coordinatesString, coordinates, longitude, latitude;
coordinatesString = '(25.229355, 55.302734)';
coordinates = coordinatesString.substring(1, coordinatesString.length - 1).split(', ');
longitude = coordinates[0];
latitude = coordinates[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.