i am quite new to javascript,
Lets say i have this:
const text = "1. foo. 2. bar. 3. baz";
this is a json returned from calling api,
is there any way to add a new line after a number index so that i can look like this when output:
- foo.
- bar.
- baz.
i want to make it like a
i tried to split by "." but it does not work and repeatedly giving
1.foo
1.foo
3.baz
3.baz
is there any way to do that?
2
Answers
You could use a regex and replace all numbers with a white spaces before and a dot after with a new line + the match characters. Also you would probably want to trim the prefix white spaces:
The OP’s required result can be achieved by a simple
replace
task which …/.s+(d+)/g
) by "a dot, a new line and the captured digit sequence" ('.n$1'
).