jQuery, removing brackets from string - javascript

I have a string like (12.131883, -68.84942999999998) and with using .replace() I wish to remove the brackets, or get the values between the brackets. A simple latlon = latlon.replace('(',' '') is not working.
Also tried it with latlon.replace(/\(.*?\)\s/g, '') but no luck.
Can anyone help me out here?

You can use substring:
var myString = "(12.131883, -68.84942999999998)";
var latlong = myString.substring(1, myString.indexOf(')'));
Or:
var myString = "(12.131883, -68.84942999999998)";
var latlong = myString.substring(myString.indexOf('(') + 1, myString.indexOf(')'));

You can get them in an array with:
var latlon = "(12.131883, -68.84942999999998)";
var ll = latlon.match(/[-+]?[0-9]*\.?[0-9]+/g)
console.log(ll) // returns ["12.131883", "-68.84942999999998"]

Related

replace parenthesis in javascript

How do you replace parenthesis with javascript.
I have a format like this:
(14.233,72.456),(12.4566,45.345),(12.456,13.567)
How can I get a format like given below:
14.233,72.456#12.4566,45.345#12.456,13.567
I have tried the following:
bounds = bounds.replace(/\)\,\(/g,'#');
bounds = bounds.replace(/\(/g,'');
bounds = bounds.replace(/\)/,'');
Splitting the string up by the delimiters and join them with your new delimiter:
var data = "(14.233,72.456),(12.4566,45.345),(12.456,13.567)";
data = data.slice(1, -1).split('),(').join('#');
Or using RegEx:
var data = "(14.233,72.456),(12.4566,45.345),(12.456,13.567)";
data = data.slice(1, -1).replace(/\),\(/g, '#');
You may try this (matches only float numbers) :
var s = '(14.233,72.456),(12.4566,45.345),(12.456,13.567)';
bounds = s.match(/\d+\.\d+,\d+\.\d+/g).join('#');
s.match(/\d+\.\d+,\d+\.\d+/g) returns :
['14.233,72.456', '12.4566,45.345', '12.456,13.567']
In addition, you might need to deal with an empty string :
bounds = (s.match(/\d+\.\d+,\d+\.\d+/g) || []).join('#');
var string = "(14.233,72.456),(12.4566,45.345),(12.456,13.567)";
var str = string.substring(1, string.length-1).split('),(').join('#');
alert(str);
Try this, which is almost the same as your code:
bounds = bounds.replace(/\),\(/g,'#').replace(/^\(|\)$/g,'');
See here the code working: http://jsfiddle.net/K8ECj/
[Edited to eliminate capture]
You can use .replace("(", "").replace(")", "");

How to get desired javascript string

I have a JavaScript string sentrptg2c#appqueue#sentrptg2c#vwemployees#
I want to get last part of the string: vwemployees through RegExp or from any JavaScript function.
and also want to remove that last keyword from string so that next time string will be like this sentrptg2c#appqueue#sentrptg2c#
I have tried
var str = "sentrptg2c#appqueue#sentrptg2c#vwemployees#";
var array = url.split('/');
var lastsegment = array[array.length-1];
and get vwemployees last segment but the string remains the same
sentrptg2c#appqueue#sentrptg2c#vwemployees#
It should be sentrptg2c#appqueue#sentrptg2c# when above code runs.
Please suggest a way to do this in JavaScript
JSFiddle: http://jsfiddle.net/satpalsingh/ykBCG/
var str = "sentrptg2c#appqueue#sentrptg2c#vwemployees#";
//Assuming # as seperator
var array = str.split('#');
//Clear empty value in array
var newArray = array.filter(function(v){return v!==''});
var lastsegment = newArray[newArray.length-1];
alert(lastsegment);
//output is "vwemployees"
<script type="text/javascript">
var str = "sentrptg2c#appqueue#sentrptg2c#vwemployees#";
var arr = str.split("#");
alert(arr[arr.length-2]);
</script>
split function will do the job for you.
use split() , splice() to remove from array and join() to join them back again
var str="sentrptg2c#appqueue#sentrptg2c#vwemployees#";
var reqvalue=str.split('#');
alert(reqvalue[3]);
reqvalue.splice(3,1);
alert(reqvalue.join('#'))
fiddle here
The split function can help you: http://www.w3schools.com/jsref/jsref_split.asp
yourString.split("#");
You will get an array with your values: sentrptg2c,appqueue,sentrptg2c,vwemployees
After that you just have to remove the last element, and rebuild your string from this array.
var str = "sentrptg2c#appqueue#sentrptg2c#vwemployees#";
alert(str);
var arr = str.split('#');
var lastsegment = arr[arr.length-2];
alert(lastsegment);
var new_str = str.replace(lastsegment+'#', '');
alert(new_str);

Search through string with Javascript

Say I have a string like this:
jJKld-xxx-JKl122
Using javascript, how can I get on what's in-between the - characters? In others words, all I need to do is put whatever is xxx into a variable.
Thanks
If the string is always in that format, this will work:
var foo = 'jJKld-xxx-JKl122';
var bar = foo.split('-')[1]; // = xxx
just try it with this simple regex
var str = 'jJKld-xxx-JKl122';
var xxx = str.replace( /^[^\-]*-|-[^\-]*$/g, '' );
You can simply use the following regex to get the result
var myString = "jJKld-xxx-JKl122";
var myRegexp = /(?:^|\s*)-(.*?)-(?:^|\s*)/g;
var match = myRegexp.exec(myString);
alert(match[1]);
See the demo here

get particular string part in javascript

I have a javascript string like "firstHalf_0_0_0" or secondHalf_0_0_0". Now I want to get the string before the string "Half" from above both strings using javascript.Please help me.
Thanks.
var myString = "firstHalf_0_0_0";
var parts = myString.split("Half");
var thePart = parts[0];
var str = 'firstHalf_0_0_0',
part = str.match(/(\w+)Half/)[1];
alert(part); // Alerts "first"
var str = "firstHalf.....";
var index = str.indexOf("Half");
var substring = str.substr(0, index);
jsFiddle demo.
Using this you can get any particular part of string.
var str= 'your string';
var result = str.split('_')[0];
Working example here for your particular case.
http://jsfiddle.net/7kypu/3/
cheers!

Substr and explode in JavaScript

I have this:
var result = "(55.6105023, 12.357556299999942)";
I would like to remove the brackets, and i thought that you could do this with substr (remove the first and last char in the string) <- But i can only manage to remove the last char if i know the length.
And then i would like to put the first number 55.6105023 into variable lat and the second 12.357556299999942 to variable lng by using explode() <- but this does not exists in JS
How can i do this?
Use slice(), which can take negative indices and unlike substr() is standardized and works in all browsers:
result.slice(1, -1);
You can use split() for the other part:
var parts = result.slice(1, -1).split(", ");
var lat = parts[0], lng = parts[1];
Alternatively you could use a regex:
var res = /^\(([\d.]+),\s*([\d.]+)\)$/.exec(result);
var lat = res[1], lng = res[2];
Remove the first and last characters by using substr, replace or slice. Use split to split the string into an array on the comma. Like so:
var chunks = result.slice(1, -1).split(", ");
var lat = chunks[0].trim();
var lng = chunks[1].trim();
I've added the trim to make sure all whitespaces are gone.
You can use a regular expression to pull out the numbers:
"(55.6105023, 12.357556299999942)".match(/[\d\.-]+/g);
// returns ["55.6105023", "12.357556299999942"]
The regex will match digits, decimal points and minus signs.
var result = "(55.6105023, 12.357556299999942)";
var substring = result.substring(1, result.length - 1);
var coordinates = substring.split(",");
var lat = coordinates[0];
var lng = coordinates[1];
You can use string manipulation functions like split and slice to parse out the numbers (or even a regular expression with matching groups), and the Number constructor to parse a number from a string:
var parseResult = function(s) {
var ss = s.split(', ');
return [Number(ss[0].slice(1)), Number(ss[1].slice(0, -1))];
};
var x = parseResult('(55.6105023, 12.357556299999942)');
x[0]; // => 55.6105023
x[1]; // => 12.357556299999942
You can do it by using substring and indexOf:
var result = "(55.6105023, 12.357556299999942)";
var lat = result.substring(result.indexOf('(')+1, result.indexOf(','));
var lon = result.substring(result.indexOf(', ')+1, result.indexOf(')'));
alert(lat); // alerts 55.6105023
alert(lon); // alerts 12.357556299999942
Here is another way, which first converts the data into proper JSON and then into a JavaScript array:
str = str.replace('(', '[').replace(')', ']');
var data = JSON.parse(str),
lat = data[0],
lng = data[1];

Categories

Resources