replace parenthesis in javascript - 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(")", "");

Related

Regex get all content between brackets

Hello i've this string ["type",[129,167,85,83]] that i want to extract only :
[129,167,85,83] using regexpr
I tried with the following :
var re = new RegExp('",(.*)]');
var r = '["type",[129,167,85,83]]'.match(re);
if (r)
console.log(r);
But this gives me the following result :
",[129,167,85,83]]
please how could i fix that ?
Not necessarily the best solution, but the quickest from where you are now. Match produces an array - you want the second item:
var re = new RegExp('",(.*)]');
var r = '["type",[129,167,85,83]]'.match(re);
if (r) console.log(r[1])
Here you go.
the trick was that adding (?<=,) will execlude comma.
Added a test below, see for your self
var regex = /(?<=,)(\[.*?\])/g;
var json = '["type",[129,167,85,83]]';
var r = json.match(regex);
console.log(r);
You can use JSON.parse
let str='["type",[129,167,85,83]]';
let arr=JSON.parse(str);
arr.shift();
let new_str=JSON.stringify(arr.flat());
console.log(new_str);
You can .split() characters followed by [ and closing ], get element at index 1
let str = `["type",[129,167,85,83]]`;
let [,match] = str.split(/.(?=\[)|\]$/);
console.log(match);
Probably best to use JSON.parse.
I have made a little example for you below;
var course = '["type",[129,167,85,83]]';
var resultArray = JSON.parse(course);
console.log(resultArray[1]); // 129,167,85,83
try this
var re = new RegExp('(?<=",)(.*)(?=])')

jQuery, removing brackets from string

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"]

Extract url from javascript String

I would like to extract the following String :
http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg
from this String :
https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg
And before, extract, i would like to check if the global String contains more than one time "http" to be sure to extract the jpg only when needed.
How can i do that ?
Extract the data like this:
var myStr = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg"
var splittedStr = myStr.split("-");
var extractedStr = splittedStr[3].slice(1);
To find out how many "http" is present in the string:
var count = (myStr.match(/http/g)).length;
Hopes it helps
var source = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg"
var temp = source.replace("https","http").split("http");
var result = 'http'+temp[2];
use split()
var original = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg";
original = original.split('-/');
alert($(original)[original.length-1]);
your require URL shows in alert dialog
You can use regex :
str.match(/(http?:\/\/.*\.(?:png|jpg))/i)
FIDDLE

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!

Quick Problem - Extracting numbers from a string

I need to extract a single variable number from a string. The string always looks like this:
javascript:change(5);
with the variable being 5.
How can I isolate it? Many thanks in advance.
Here is one way, assuming the number is always surrounded by parentheses:
var str = 'javascript:change(5);';
var lastBit = str.split('(')[1];
var num = lastBit.split(')')[0];
Use regular expressions:-
var test = "javascript:change(5);"
var number = new RegExp("\\d+", "g")
var match = test.match(number);
alert(match);
A simple RegExp can solve this one:
var inputString = 'javascript:change(5);';
var results = /javascript:change\((\d+)\)/.exec(inputString);
if (results)
{
alert(results[1]); // 5
}
Using the javascript:change part in the match as well ensures that if the string isn't in the proper format, you wont get a value from the matches.
var str = 'javascript:change(5);', result = str.match(/\((\d+)\)/);
if ( result ) {
alert( result[1] )
}

Categories

Resources