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
Add a ment  | 

3 Answers 3

Reset to default 7

Use 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