Sol Sol - 2 months ago 6
Javascript Question

How do you replace characters in Javascript?

How can you in Javascript replace characters? I need to replace '(' and ')' in this output to make this link work correctly. Jquery would be ok too, but Javascript is usually faster on it's own.

function success(position) {
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var latlng1 = latlng.replace('(', '');
var latlng1 = latlng1.replace(')', '');

alert("http://www.google.com/maps/@"+latlng1+",17z");
}


For example my output of latlng (latitude, longitude) would show the ( ) in it:

(48.6617564,-102.98313889933997)

Answer

You could try the following regex

 var latLng = "(48.6617564,-102.98313889933997)",
    latLng = latLng.replace(/([()])/g, '');

 console.log("http://www.google.com/maps/@"+latLng+",17z");