Find the range bounded by ()
from the string passed by the argument str
and return the string for that part.
()
returns empty characters if the contents are empty.()
returns the contents of the first parenthesis found if there are multiple ()
(Quickest match) ()
is not found.If ()
is empty, I want to return an empty character, but it says null.
Could you please let me know?
function extractStr(str){
varret=/\(.+?)\)/.exec(str);
return ret?ret[1]: null;
}
If the contents of ( ) are empty, I want to return empty characters, but it says null.
The regular expression is \(.+?)\)
, and (.+?)
is specified in parentheses. +?
is one or more characters long, so if () is empty, does not apply and fails.The regular expression must be \(.*?)\)
using *?
for zero or more characters if you want it to succeed even if it is empty.
© 2024 OneMinuteCode. All rights reserved.