Get value between brackets

Jane

New Member
#1
I have:
Mã:
item = "[a,b,c,d]"
I want the value between []:
Mã:
"a,b,c,d"
I tried:
Mã:
item.split(/,/)
but this does not work. I tried split/each but neither works.
 

Admin

Administrator
Thành viên BQT
#2
Mã:
> item.delete('[|]')
#=> "a,b,c,d"
To override self value you can use delete!

Note: It only delete brackets from your string, if your value can be anything and you want to fetch only string within brackets then use:
Mã:
> item = "hi [a,b,c,d] world"
> item[/(?<=\[).*(?=\])/]
# OR
> item[/\[(.*)\]/, 1]
#=> "a,b,c,d"
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top