Regular Expression to get a string between parentheses in Javascript
You need to create a set of escaped (with \
) parentheses (that match the parentheses) and a group of regular parentheses that create your capturing group:
var regExp = /\(([^)]+)\)/;var matches = regExp.exec("I expect five hundred dollars ($500).");
//matches[1] contains the value between the parenthesesconsole.log(matches[1]);
Regular expression to get value in between parentheses
You may do that using
value[/\((.*?)\)/, 1]
or
value[/\(([^()]*)\)/, 1]
Use a capturing group and a second argument to extract just the group value.
Note that \((.*?)\)
will also match a substring that contains (
char in it, and the second option will only match a substring between parentheses that does not contain (
nor )
since [^()]
is a negated character class that matches any char but (
and )
.
See the Ruby demo online.
From the Ruby docs:
str[regexp, capture] → new_str or nil
If aRegexp
is supplied, the matching portion of the string is returned. If a capture follows the regular expression, which may be a capture group index or name, follows the regular expression that component of theMatchData
is returned instead.
In case you need to extract multiple occurrences, use String#scan
:
value = "John sinu.s(14) and Jack(156)"
puts value.scan(/\(([^()]*)\)/)
# => [ 14, 156 ]
See another Ruby demo.
JavaScript: How to get string between brackets
Both of these examples assume that the string will always have a set of parenthesis, opening before closing.
I suggest using substring and indexOf for this:
var result = myString.substring( myString.indexOf( '(' ) + 1, myString.indexOf( ')' ) );
You can also use a regex if you prefer:
var result = /\(([^)]*)\)/.exec(myString)[1];
Regex to select text between parentheses with a specific text as target
You only have to include the target string surrounded by character classes that exclude the closing parenthesis:
\(([^)]*TARGET[^)]*)\)
If you only need to replace the match, you don't need the capture group (you can remove it).
RegEx to match stuff between parentheses
You need to make your regex pattern 'non-greedy' by adding a ?
after the .+
By default, *
and +
are greedy in that they will match as long a string of chars as possible, ignoring any matches that might occur within the string.
Non-greedy makes the pattern only match the shortest possible match.
See Watch Out for The Greediness! for a better explanation.
Or alternately, change your regex to
\(([^\)]+)\)
which will match any grouping of parentheses that do not, themselves, contain parentheses.
Extract all chars between parenthesis
Don't use a negative character set, since parentheses (both (
and )
) may appear inside the match you want. Greedily repeat instead, so that you match as much as possible, until the engine backtracks and finds the first )
from the right:
console.log(
'aaaaa (test(())) bbbb'
.match(/\(.*\)/)[0]
);
Regular Expression to get a string between parentheses and inside the string literal in Javascript
let s = "Cannot add or update a child row: a foreign key constraint fails (`testdb`.`products`, CONSTRAINT `products_ibfk_3` FOREIGN KEY (`category_id`) REFERENCES `product_categories` (`id`))";
let match = /FOREIGN KEY \(`([\w]*)`\)/.exec(s);
let extracted = match[1];
console.log(extracted);
Get string inside parentheses, removing parentheses, with regex
Like this
Javascript
var str1 = "this is (1) test",
str2 = "this is (2) test",
str3 = str1.match(/\((.*?)\)/)[1] + str2.match(/\((.*?)\)/)[1];
alert(str3);
On jsfiddle
See MDN RegExp
(x) Matches x and remembers the match. These are called capturing
parentheses.For example, /(foo)/ matches and remembers 'foo' in "foo bar." The
matched substring can be recalled from the resulting array's elements
1, ..., [n] or from the predefined RegExp object's properties $1,
..., $9.Capturing groups have a performance penalty. If you don't need the
matched substring to be recalled, prefer non-capturing parentheses
(see below).
string between parentheses with regex
You need to work with capture regex groups ()
to withdraw the number and string separately, have a look:
let rawStr = "this is (131PS) for now";
let theMatch = rawStr.match(/\((\d+)([A-Z]+)\)/);
if (theMatch) { let theNum = parseInt(theMatch[1]); let theString = theMatch[2]; console.log(theNum, theString);}
Get text between parentheses
This should do the trick:
(?:\()[^\(\)]*?(?:\))
How it works
\(
matches opening brackets\)
matches closing brackets[^\(\)]
matches anything apart from more brackets*?
add a zero or more times quantifier to the above pattern (made lazy by?
)
JavaScript implementation
Try:
your_string.match(/(?:\()[^\(\)]*?(?:\))/g)
Related Topics
Array.Fill(Array) Creates Copies by References Not by Value
How to Get the Size of a JavaScript Object
Regular Expression to Get a String Between Parentheses in JavaScript
Convert Utc Epoch to Local Date
Where Do You Include the Jquery Library From? Google JSAPI? CDN
How to Convert Url Parameters to a JavaScript Object
Generic Deep Diff Between Two Objects
How to Get Element by Innertext
Why Is [1,2] + [3,4] = "1,23,4" in JavaScript
Center a Popup Window on Screen
Constructor Function VS Factory Functions
Why Is My Function Call That Should Be Scheduled by Settimeout Executed Immediately
How to Download a File with Angular2 or Greater
Add Directives from Directive in Angularjs
Check Whether Variable Is Number or String in JavaScript