admin管理员组文章数量:1388559
Trying to get "Table 2" or whatever table number it might be (sometimes 2 digits) into a variable. Any idea why this is returning null
?
var productText = '25-08-12 Boat Cruise (Table 2)';
var rgx = /^\(\)$/;
var newText = productText.match(rgx);
alert(newText);
Trying to get "Table 2" or whatever table number it might be (sometimes 2 digits) into a variable. Any idea why this is returning null
?
var productText = '25-08-12 Boat Cruise (Table 2)';
var rgx = /^\(\)$/;
var newText = productText.match(rgx);
alert(newText);
Share
Improve this question
edited Sep 23, 2012 at 0:06
Mike Samuel
121k30 gold badges227 silver badges254 bronze badges
asked Sep 22, 2012 at 23:43
Graham MorleyGraham Morley
4501 gold badge9 silver badges20 bronze badges
3
- 1 no offense, but the real answer to "Why?" is "You have no idea what you are doing." Someone will probably post a properly formed regex for this particular problem but you might want to take a step back and do some reading/prehension on regular expressions in general before trying to apply it to even a trivial problem. See Cargo-Cult Programming – J. Holmes Commented Sep 22, 2012 at 23:48
- 1 Of course posting an answer with an answer which isn't a solution but instead posting a link to a 'famous' programmer isn't being fanboy or cargo-cult. – SonOfNun Commented Sep 22, 2012 at 23:54
- You're right, I don't have much idea how to use regex. I tried to do some reading and figure it out, and this is the results I got. Sorry 32bitkid, but sometimes in the interest of time it's better to ask of the helpful munity than spend 2 extra hours on something. – Graham Morley Commented Sep 23, 2012 at 0:18
3 Answers
Reset to default 7Use the following instead:
var rgx = /\(([^)]+)\)/;
var match = productText.match(rgx);
var newText = match && match[1];
// newText's value will be "Table 2" if there is match; null otherwise
When you have /^\(\)$/
, you are actually trying to match the string "()"
, no more, no less. Instead, you should match, anywhere in the text, a (
, store everything between it and the next )
in a capturing group ([^)]+)
, so that you can refer to the captured group later on with match[1]
.
If you want just the number, use /\(Table (\d+)\)/
.
var rgx = /\((.*)\)/
Will capture the table number into a group.
Your regex currently says
'give me () at the beginning (^) and the end of the string ($)'.
The ^ means a line start and
the $ means a line end.
Also, you need something to match the text inside the ()
, for example: .*
Having that pointed, /^\(\)$/
would only match to ()
.
Then, a working example could be:
var productText = '25-08-12 Boat Cruise (Table 2)';
var rgx = /\(.*\)/;
var newText = productText.match(rgx)[0];
newText = newText.replace('(','');
newText = newText.replace(')','');
alert(newText);
After seeing what you needed, I would remend the use of jQuery's data.
本文标签: javascript regex returning nulltrying to get text between parenthesesStack Overflow
版权声明:本文标题:javascript regex returning null, trying to get text between parentheses - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744557989a2612593.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论