Reputation: 183
Hi i need some help to remove last curly braces, } from a dynamic string.
I recieve a dynamic string that can be longer or smaller sometimes, but i need to remove the last } in the string
I was thinking of using regex, but how can i acchieve this with a dynamic string that changes?
Thanks
Upvotes: 1
Views: 1091
Reputation: 76408
dystroy's answer is correct, but in the interest of completeness: you could use a negative look-ahead assertion, too:
var s = 'f}oo}bar';
s = s.replace(/}(?!.*})/, '');
console.log(s);//f}oobar
Basically this regex replaces a single closing curly brace, if it's the last (or only) }
char in the string.
how it works:
}
: matches a literal } char(?!
negative look-ahead: matches the }
only if it is not followed by:.*}
: zero or more chars, followed by a closing curlyUpvotes: 3
Reputation: 382474
You can do this to remove the last }
of a string :
s = s.replace(/\}([^}]*)$/,'$1')
Upvotes: 5