javascript - RegEx start with, contains these, not end with? -
i need regular expression find comparison parts in following example.
var magicalregex = ""; var example = '({color}=="red"|{color}=="yellow")&{size}>32'; example.replace(magicalregex, function (matchedblock) { console.log(matchedblock); }); //so want see following result on console //{color}=="red" //{color}=="yellow" //{size}>32 in fact did things couldn't complete, may check following template couldn't complete.
\{.*?(==|>) https://regex101.com/r/aoddex/1
thanks
answer
according example have on regex101 string have in code snippet (two different strings) following regex want.
answer 1
({.*?}(?:==|>)(?:\d+|(?:(["']?).*?\2))) you can see regex in use here
answer 2
note i've added both single , double quotes in above regex. if need double quotes, use following regex.
({.*?}(?:==|>)(?:\d+|".*?")) you can see regex in use here
explanation
these regular expressions work follows:
- match
{, followed character (except newline) number of times, few matches possible, followed} - match
==or> - match digit 1 unlimited times or match quoted string (any character number of times, few matches possible) e.g.
"something"
the regex captures entire section , if @ examples on regex101 presented, can see each capture group matching. can remove capture groups if not intended use.
expected results
input
note 2 strings below used testing purposes. 1 string present in question , other present in link provided op.
({renk}=="kirmizi"or{renk}=="sari")or{size}>32 ({color}=="red"|{color}=="yellow")&{size}>32 output
note output mentioned hereafter specifies matched/also capture group 1 (since whole regex in capture group). other groups disregarded not important overall question/answer.
{renk}=="kirmizi" {renk}=="sari" {size}>32 {color}=="red" {color}=="yellow" {size}>32 
Comments
Post a Comment