Ruby regex replace all but what's within parenthesis
How do I replace all but what's within parenthesis
For instance:
some_html=some_html.gsub(/<a href=\"([^\"]+)\"/, "");
now I would like to replace that string with: whatever 开发者_开发知识库is in the parenthesis.
I don't really understand your question, so if it needed feel free to correct me.
I assume you need to transform following <a href="needless[target]trash"
into something like <a href="target"
So following code do the job:
some_html.gsub!(/(<a href=")[^"]*\[([^"]+)\][^"]*"/, '\\1\\2"')
精彩评论