regular expression problem

var text="Afganistan (+86)"
var code=text.sub(/\w+/, '')
result: code = (+86)

HUNT HUNT wrote:

var text="Afganistan (+86)"
var code=text.sub(/\w+/, '')
result: code = (+86)
------------------------------

var text = "Antigua and Barbuda (+1268)"
var code=text.sub(/\w+/, '')
result : code = and Barbuda (+1268)"

--------------------------------------
what regular expression I can try to get second one as first. ie (+1268)

Do you want "(+1268)" from the second query? If so, does

var code=text.sub(/[\w+ ]/, '')

work? See http://rubular.com/r/g5I6tYxb5C for where I show it works.

Thanks,
ben

“Antigua and Barbuda (+1268)”.scan(/\d+/).to_s

=> 1268

“Antigua and Barbuda (+1268)”.scan(/(+\d+)/).to_s

=> (+1268)

Hi --

nice one David!

Vladimir Rybas wrote:

nice one David!

I did with this

var code=text.sub(/[a-z A-Z]/, '')

David A. Black wrote:

Hi --

"Antigua and Barbuda (+1268)".scan(/\d+/).to_s
=> 1268

"Antigua and Barbuda (+1268)".scan(/\(\+\d+\)/).to_s
=> (+1268)

There's a nice technique for quickly getting a substring from a
string using a subscript-style notation:

   "Antigua and Barbuda (+1268)"[/\(\+\d+\)/] # "(+1268)"

Ooh, nice! Didn't know about that.

David

Best,