0

How to replace text before specific character?

I need to replace a text before '{' character.

([a-z]+)\s?\{

Text: text {
Replaced by: test
Result: test

How to get a result like test {?

Thanks in advance.

1
  • 'test{' + 'text{dsdsads'.split('{')[1]; Commented Jun 17, 2014 at 17:45

2 Answers 2

2

How about this: /[\w\s]+(?={)/

Then you could replace the "text" before the { by doing something like:

"test2{test1".replace(/[\w\s]+(?={)/,'demo');
Sign up to request clarification or add additional context in comments.

Comments

2

You could use a negated [^ ] match combined with a Positive Lookahead.

'text {'.replace(/[^{]+(?={)/, 'test '); //=> "test {"

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.