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);
Related
This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 3 years ago.
My string should be in the IRC command format : "/add john".
So, i created this Regex :
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test('\/add user1');
alert(bool);
The problem is either I use /***/ or RegExp syntax, if I set a backslash at the beginning of my string (like in my example above), my alert pop up show "true" and I don't want that.
I code in Javascript
You can use String.raw to make sure that the backlash is not removed when testing your input:
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test(String.raw`\/add user1`);
alert(bool);
You can play with this code here: https://jsbin.com/ziqecux/25/edit?js
This question already has answers here:
Remove characters from a string [duplicate]
(6 answers)
Closed 7 years ago.
I have the below variable in JavaScript. I want to remove the "comma" sign using jQuery /JavaScript.
var test = ",,2,4,,,3,,3,,,"
Expected output: var test = "2,4,3,3"
please advice
Use replace() with regex
var test = ",,2,4,,,3,,3,,,";
document.write(test.replace(/,+/g,',').replace(/^,|,$/g,''));
replace(/,+/g,',') - To remove multiple comma with one.
replace(/^,|,$/g,'') - To remove comma at starting and ending.
This question already has answers here:
Create RegExps on the fly using string variables
(6 answers)
Closed 8 years ago.
I need to use data attribute for regex just as
<div data-regex="REGEX-HERE">
and then get the value by javascript and put in a variable. and then do a test like
var regex = $(this).attr("data-regex");
regex.test(name)
when I tried to use "^[\x20-\x7E]+$" for testing english character is didn't work.
Note when I tried this
var regex = /^[\x20-\x7E]+$/;
It worked.
Thanks in advance
You can do this:
var regex = new RegExp("^[\x20-\x7E]+$",""); // Modifiers on the tend
So finally:
var regex = new RegExp($(this).data("regex"));
regex.test(name)
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"]
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?
I am trying to parse an xml document like this:
var str = data.match("<string>" + "(.*?)" + "</string>");
console.log(str);
I want to get all the elements between the [string] in an array but for some reason, it only returns the first string element found. Im not good with regular expressions so Im thinking this is just a small regex issue.
You want it to be global g
var str="<string>1</string><string>2</string><string>3</string>";
var n=str.match(/<string>(.*?)<\/string>/g);
//1,2,3
You have to form the RegEx adding a g to it like
/Regex/g