Retreive Information Around Key Words Javascript [duplicate] - javascript

This question already has answers here:
Regular Expression to get a string between parentheses in Javascript
(10 answers)
Closed 7 years ago.
Let's say I have a string that looks like this:
var string = "(This is an auto-generated string) and this is useless text I don't need";
What I would like to do is assign another variable the string INSIDE the parentheses so the second variable would look like this:
var selected_string = "This is an auto-generated string";
How would I retrieve data around the parentheses and then assign it to a variable in JavaScript?
Thanks!

You can use a regular expression to pull out any information between parens:
var regex = /\((.*?)\)/;
var matched = regex.exec(string);
var selectedString = matched[1];

Related

Extract text from enclosing parentheses using JavaScript [duplicate]

This question already has answers here:
Get text between two rounded brackets
(7 answers)
Closed 2 years ago.
I have a string like Manila (Philippines) and want to replace it with only the substring Philippines. I tried using the following regex pattern, which works in Notepad++:
[^\(]+ \(([^\)]+)\)
However, I get an undefined result in JavaScript:
var x = "Manila (Philippines)";
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,$1));
You just forgot the " around your replace pattern!
console.log(x.replace(/[^\(]+ \(([^\)]+)\)/,"$1")); will work correctly!
You can use .match():
var x = "Manila (Philippines)";
var result = x.match(/\((.+)\)/).pop();
// regex for string contained in parentheses
console.log(result);

Prevent back slash escaping from the string in Javascript [duplicate]

This question already has answers here:
Escaping backslash in string - javascript
(5 answers)
Closed 3 years ago.
I have a code which replaces the back slashes as empty string
var data = '<strong>Welcome</strong>\(x = {-b';
document.write(data);
I am getting result like this:
Welcome(x = {-b
I am expecting result like this without modifying string value
Welcome\(x = {-b
I used the same string to display in an id as html content.
document.getElementById("test").innerHTML = '<strong>Welcome</strong>\(x = {-b';
It always replacing slashes by empty string. the string.split method did not helped me to solve this
var data = '<strong>Welcome</strong>\\(x = {-b';
document.write(data);
try this...

How to use dynamic variable between regular expression [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 string which contains some date and some comma separated values like this
var a = "1,13,20160308,200500000012016,10,Pending,01-02-2016,1|#|1,13,20160418,200500000012016,10,Pending,08-03-2016,1|#|1,13,20160623,200500000012016,10,Pending,18-04-2016,1|#|1,13,20160803,200500000012016,10,Pending,23-06-2016,1|#|1,13,20160912,200500000012016,10,Pending,03-08-2016,1|#|1,13,20161022,200500000012016,10,Pending,12-09-2016,1|#|1,13,20161129,200500000012016,10,Pending,22-10-2016,1|#|1,13,20170110,200500000012016,10,Pending,29-11-2016,1|#|1,13,20170215,200500000012016,10,Pending,10-01-2017,1|#|15-02-2017 APPEARANCE"
regular expression: /(.)*?01-02-2016(.)*?\|\#\|/igm
By using this regular expression i can able to delete unnecessary part in string.
Now i want to change 03-08-3016 (date) dynamically. If i use
var date = "01-02-2016"
var reg = /(.)*?${date}(.)*?\|\#\|/igm;
If you pring reg in console.log you will get like this below
console.log(reg) ----> output: '/(.)?01-02-2016(.)?|#|/igm'
Expected Final output will delete upto 01-02-2016,1|#|
Use this.
var regex="(.)*?01-02-2016(.)*?\\|\\#\\|";
var rx=new RegExp(regex,"igm");
console.log(rx);
//Then when do you want to change,
regex=regex.replace("01-02-2016","03-02-2016");
rx=new RegExp(regex,"igm");
console.log(rx);
JavaScript have 2 methods to make a Regular Expression.
1. write it in slashes //
2. Make from string using new RexExp(string);
If you make it from string, you can give the constraint(" global, incase, etc.") as the second parameter as i did in the above.
and also you have to double escape (\) the escape characters.

How do I take value of a string leading up to a comma using javascript? [duplicate]

This question already has answers here:
How to grab substring before a specified character in JavaScript?
(12 answers)
Closed 6 years ago.
I am attempting to take the city out of a city, state string. For instance, "Portland, OR" would need to equal "Portland". How can I use Javascript or a regular expression to do this?
var myString = "Portland, OR";
I want to extract everything up to the comma but not including the comma.
var city = "Portland, OR".split(",")[0];
With regex:
var regex = /^[^,]+/;
var city = regex.exec("Portland, OR");
This is the regex version
var result = myString.match(/^[^,]+/)
UPDATE
This one always returns a value without error
var result = String(myString || "").match(/^[^,]*/)[0]

How to extract numbers from string in Javascript using regex [duplicate]

This question already has answers here:
Find and get only number in string
(4 answers)
Closed 8 years ago.
I have the following string
/Date(1317772800000)/
I want to use a Javascript regular expression to extract the numerical portion of it
1317772800000
How is this possible?
That should be it
var numPart = "/Date(1317772800000)/".match(/(\d+)/)[1];
No need for regex. Use .substring() function. In this case try:
var whatever = "/Date(1317772800000)/";
whatever = whatever.substring(6,whatever.length-2);
This'll do it for you: http://regex101.com/r/zR0wH4
var re = /\/Date\((\d{13})\)\//;
re.exec('/Date(1317772800000)/');
=> ["/Date(1317772800000)/", "1317772800000"]
If you don't care about matching the date portion of the string and just want extract the digits from any string, you can use this instead:
var re = /(\d+)/;
re.exec('/Date(1317772800000)/')
["1317772800000", "1317772800000"]

Categories

Resources