Javascript - split values inside parentheses [duplicate] - javascript

This question already has answers here:
How to get function parameter names/values dynamically?
(34 answers)
Closed 3 years ago.
I am trying to split the following string:
delete(value1,value2);
I want to get the values and save them in a array:
var values = [value1,value2]

A regex would do the trick:
/\((.*)\)/g.exec('delete(value1,value2);')[1].split(',')
This captures anything between parentheses, which you can then split again.

You can split the string using split function
var str = "delete(value1,value2)";
var stringArray = str.split("(");
var values=stringArray[1].split(",");
use Replace method to finally delte ")" from second string in array
values[1]=values[1].replace(")","");

How about:
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;
function getParamNames(fnStr) {
var fnStr = fnStr.replace(STRIP_COMMENTS, '');
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if(result === null)
result = [];
return result;
}
var str = "delete(value1,value2)";
getParamNames(str);

Related

How to trim string between given parameter from existing string in javascript [duplicate]

This question already has answers here:
How to remove the end of a string, starting from a given pattern?
(5 answers)
Closed last month.
I have the string "/Employee/Details/568356357938479"; and I want to obtain the new string of only "/Employee" from the given string?
var myString = "/Employee/Details/568356357938479";
var newString = myString.replace(/\\|\//g, '');
I'm expecting : "/Employee"
try this:
var myString = "/Employee/Details/568356357938479";
var newString = myString.substring(0, myString.indexOf("/", 1));
console.log(newString);

multiple string replace using variable [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 6 years ago.
I have to replace multiple words in a string.
my code is like this
var csku = "{len}";
var value = 5;
var finalPrice = "({con}*{len})+{wid}+{fixed_var}+{extra}+{sqft}+{len}";
console.log(finalPrice.replace(csku, value));
Using this code I got this solution
({con}*5)+{wid}+{fixed_var}+{extra}+{sqft}+{len}
but I want this
({con}*5)+{wid}+{fixed_var}+{extra}+{sqft}+5
I google it for replacing multiple words in the string using one call I find this
str.replace(/X|x/g, '');
Here / and g is used for multiple replace and in this format I have to add static word but in my code csku is not fixed so how can I replace all words in one call using variable
Using the code from the duplicate to create a Regex object using the variable
var csku = "{len}";
var value = 5;
var finalPrice = "({con}*{len})+{wid}+{fixed_var}+{extra}+{sqft}+{len}";
var re = new RegExp(csku, "g");
console.log(finalPrice.replace(re, value));
use new RegExp(cksu, 'g') to create a regular expression that'll match all cksu.
new RegExp('{len}', 'g') will return /{len}/g meaning all global matches.
so finalPrice.replace(new RegExp(cksu, 'g'), value) will replace all global matches of cksu with value.
var csku = "{len}";
var value = 5;
var finalPrice = "({con}*{len})+{wid}+{fixed_var}+{extra}+{sqft}+{len}";
console.log(finalPrice.replace(new RegExp(csku, 'g'), value));
Standart replace function change just first match. You can use this function:
function ReplaceAll(Source, stringToFind, stringToReplace) {
var temp = Source;
var index = temp.indexOf(stringToFind);
while (index != -1) {
temp = temp.replace(stringToFind, stringToReplace);
index = temp.indexOf(stringToFind);
}
return temp;
}

pass variable to replace function regular expression javascript [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Closed 6 years ago.
I want to have a 'newline'-function to pass a string to it and print it on pdf. my function so far is
var array = new Array();
function newLineFunction_PDF(text) {
var arr = text.replace(/.{70}\S*\s+/g, "$&#").split(/\s+#/);
return arr;
}
array = newLineFunction_PDF('some Text');
for( var i in array) {
print(array[i]);
}
What it does is cut the text in to pieces of length-70 incl. the last word, push it into the array and print it afterwards with new lines. Now i want to pass a number to the function, like 100, so i can decide the max-length of the text per line.
So far I tried:
function newLineFunction_PDF(text, num) {
var re = new RegExp(/.{num}\S*\s+/g);
var arr = text.replace(re, "$&#").split(/\s+#/);
return arr;
}
but I dont know how and where to add escapes into the new RegExp.
The parameter of Regexp is a string:
var re = new RegExp('.{' + num + '}\S*\s+', 'g');

parse extra information in a link with javascript [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 8 years ago.
i have this link output from a facebook feed:
http://l.facebook.com/l.php?u=http%3A%2F%2Fwww.theguardian.com%2Ftravel%2F2014%2Fapr%2F25%2Fitaly-puglia-salento-region&h=2AQF4oNrg&s=1
and i need the final ouput like this:
http://www.theguardian.com/travel/2014/apr/25/italy-puglia-salento-region
so basically i have this bit to remove:
http://l.facebook.com/l.php?u=
i was trying a regex in javascript but not very familiar with it:
Body = document.getElementsByTagName("body")[0].innerHTML;
regex = ???
Matches = regex.exec(Body);
any ideas on how to make it work?
Use this :
function extractLinkFromFb(fbLink) {
var encodedUri = fbLink.split('?u=');
return decodeURIComponent(encodedUri[1]);
}
var link = 'http://l.facebook.com/l.php?u=http%3A%2F%2Fwww.theguardian.com%2Ftravel%2F2014%2Fapr%2F25%2Fitaly-puglia-salento-region&h=2AQF4oNrg&s=1';
var myExtractedLink = extractLinkFromFb(link);
The function extractLinkFromFb() will return your link.
Before decoding the url, you can use this regex to grab the piece you want:
var myregex = /u=([^&#\s]+)/;
var matchArray = myregex.exec(yourString);
if (matchArray != null) {
thematch = matchArray[1];
} else {
thematch = "";
}
The parentheses in the regex captures the match to Group 1
u= serves as a delimiter, but is not captured in Group 1
[^&#\s] matches one character that is not a &, # or whitespace character. Tweak to suit.
the + quantifier matches one or more of these characters
var url = 'http://l.facebook.com/l.php?u=http%3A%2F%2Fwww.theguardian.com%2Ftravel%2F2014%2Fapr%2F25%2Fitaly-puglia-salento-region&h=2AQF4oNrg&s=1';
var str1 = "http://l.facebook.com/l.php?u=";
var str2 = "&h=2AQF4oNrg&s=1";
var url = url.replace(str1, "");
var url=url.split("&h=")
var uri_dec = decodeURIComponent(url[0]); // =http://www.theguardian.com/travel/2014/apr/25/italy-puglia-salento-region
//alert (uri_dec);
//console.log (uri_dec);

JavaScript Find and replace with variable in regular expression [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular Expression Pattern With A Variable
function function1() {
var key = "name";
var sample = "param.name['key'] = name; param.name[i] = 1000; param.name1[i] = name1;";
var result = result.replace(/param.<<name>>\[(\d+)\]/g, 'parameter[prefix_$1]');
}
Expected result: parameter['prefix_key'] = name; parameter['prefix_i'] = 1000;
I cant add variable key into the replace function in regular expresssion.
Please help how to construct the regular expression in replace
You can make a regex out of a string by making a RegExp object:
var regex = new RegExp("param\\." + name + "\\[(\d+)\\]", "g")
var result = result.replace(regex, 'parameter[prefix_$1]');

Categories