1

My String is

var str = "{ aaaa} bbb} ccc ] } ddd } eeee"

I want newString as by using last occurance of }

var newString = "{ aaaa} bbb} ccc ] } ddd }"

How to do it?

3 Answers 3

2

Try to use lastIndexOf:

var s = '{ aaaa} bbb} ccc ] } ddd } eeee';
alert( s.substring(0, s.lastIndexOf('}')+1));

Sign up to request clarification or add additional context in comments.

Comments

2
var newString = str.replace(/ \w+$/, '');

3 Comments

@Group : he did exactly what you asked for... you don't believe, here comes the fiddle - jsfiddle.net/8esw7t1L
@Oxi event though it works for the given string, he asked for the last occurence of {, not for the last "space + chars" sequence
@JeromeWAGNER : i get it now, thanks. small correction in your comment, it is "last occurence of }" NOT "last occurence of {" :)
1
var newString = str.substr(0, str.lastIndexOf('}')+1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.