开发者

Make one gsub call instead of five

开发者 https://www.devze.com 2022-12-15 08:02 出处:网络
How can I r开发者_JS百科eplace this: lyrics = lyrics.gsub(/\\n/,\'\').gsub(/^\\{\\\"similar\\\": \\[/, \'\').gsub(/\\]\\}$/, \'\').gsub(/^\\{/, \'\').gsub(/\\}$/, \'\')

How can I r开发者_JS百科eplace this:

lyrics = lyrics.gsub(/\n/,'').gsub(/^\{\"similar\": \[/, '').gsub(/\]\}$/, '').gsub(/^\{/, '').gsub(/\}$/, '')

to something shorter and one gsub call?


You can joint multiple regexes into one by using alternate symbol | and creating branches in regex. Pay attention to anchors like ^, $ and other, because if they appear in one branch, they only work for that branch, not whole regex

lyrics = lyrics.gsub(/\n|^\{\"similar\": \[|\]\}$|^\{|\}$/, '')
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号