1

I have a string that looks like the following:

"01/11/2012 (Last, First) - Notes,02/11/2012 (Last, First) - More Notes,03/11/2012 (Last, First) - Even More Notes,09/12/2012 (Last, First) - You get the idea"

I would like each note to fall on a new line by replacing any comma followed by a number with '\n'.

I have tried .replace(/,/g, '\n') but I get a newline in the middle of the name.
So I do .replace(/,\d/g, '\n'), however then I loose the first number of the date.

How can I search for ,# and replace it with \n# where the number is the same as it was previously?

I want the result to look like this:

01/11/2012 (Last, First) - Notes
02/11/2012 (Last, First) - More Notes
03/11/2012 (Last, First) - Even More Notes
09/12/2012 (Last, First) - You get the idea

2 Answers 2

2

You can use a capture to avoid missing that number:

s.replace(/,(\d)/g, '\n$1')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I had done this in the past, I just was having trouble finding the syntax anywhere. Monday morning Google skill need some work :)
No problem :). All Monday mornings always need some tuning of ourselves.
2

(Update, sorry, just noticed that you don't want the comma)

Try...

.replace(/,(\d)/g, '\n$1')

This groups the \d and then uses it (with $1) in the replace

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.