Tell me, in the writing of the regular season. It is necessary to remove any number of zeros before the first unit (leave one) and remove all 0 and 1 before a . Latex expression - 01 ^ {01a} There is such a sketch https://regex101.com/r/yE5sJl/2 , but it removes { and latex breaks.

  • I need more data to check, I got this: ^[0]+|[^^{]+[01]+ - MrFylypenko
  • There is an input that receives a latex expression - it can be 3 options: 01 ^ {a} , 1 ^ {01a} , 01 ^ {01a} . It is necessary before the unit (the first , to leave it ) delete all 0 , and before a , delete 0 and 1 if they are. - Phaeton
  • then like this: ^[0]*|(?!1\^{)[01]*|(?!a})$ - MrFylypenko
  • one
    for your examples, if you delete what you wrote, you always get 1^{a} why delete something, if you can just replace it with this line? or write more examples - teran

1 answer 1

Use

 s.replace(/^0+|({)[01]+/g, '$1') 

See the demo expressions.

Details

  • ^0+ - one or more zeros at the beginning of a line
  • | - or
  • ({) - Group (disguise) №1: symbol {
  • [01]+ - 0 or 1, one or more repetitions.

Matches are replaced by the value of the first group, i.e. either by an empty string (if the first alternative matches) or by the { symbol (thus, the bracket is not removed from the string).

JS-demo:

 var strs = ['01^{0001a}','01^{a}','1^{01a}','01^{01a}']; for (var s of strs) { console.log(s, '=>', s.replace(/^0+|({)[01]+/g, '$1')); } 

If it is very, very necessary that after {01010 be a , add a forward lookup block:

 /^0+|({)[01]+(?=a)/g ^^^^^ 

See another demo .