Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

javascript - 正则表达式在大括号之间获取字符串“ {我想大括号之间是什么}”(Regex to get string between curly braces “{I want what's between the curly braces}”)

Unfortunately, despite having tried to learn regex at least one time a year for as many years as I can remember, I always forget as I use them so infrequently.(不幸的是,尽管尽我所能记住,试图每年至少学习一次正则表达式长达多年,但我却经常忘记,因为我很少使用它们。)

This year my new year's resolution is to not try and learn regex again - So this year to save me from tears I'll give it to Stack Overflow .(今年,我新年的决心是不要再尝试学习正则表达式- 因此,今年,为了让我免于眼泪,我将把它交给Stack Overflow 。) (Last Christmas remix).((去年圣诞节混音)。) I want to pass in a string in this format {getThis} , and be returned the string getThis .(我想以{getThis}格式输入字符串,并返回字符串getThis 。) Could anyone be of assistance in helping to stick to my new year's resolution?(有人能协助我们坚持我的新年决议吗?) Related questions on Stack Overflow:(有关堆栈溢出的相关问题:) How can one turn regular quotes (ie ', ") into LaTeX/TeX quotes (ie `', ``'')(如何将常规引号(即“,”)转换为LaTeX / TeX引号(即“'',“'')) Regex: To pull out a sub-string between two tags in a string(正则表达式:在字符串中的两个标签之间提取子字符串) Regex to replace all \n in a String, but no those inside [code] [/code] tag(正则表达式替换字符串中的所有\ n,但不替换[code] [/ code]标记内的所有\ n)   ask by ChrisInCambo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try(尝试)

/{(.*?)}/ That means, match any character between { and }, but don't be greedy - match the shortest string which ends with } (the ? stops * being greedy).(这意味着,匹配{和}之间的任何字符,但不要贪婪-匹配以}结尾的最短字符串(?停止*为贪婪)。) The parentheses let you extract the matched portion.(括号使您可以提取匹配的部分。) Another way would be(另一种方式是) /{([^}]*)}/ This matches any character except a } char (another way of not being greedy)(这匹配除} char以外的任何字符(不贪婪的另一种方式))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...