lel lel - 25 days ago 12
Javascript Question

Add in value before single quote (')

I want to add in value before the end of a single quote. Please do help me thanks!

var example = [['2019-02-10', 6.2]],[['2019-03-10', 6.2]]

After replacement

var example = [['2019-02-10 0:00AM' , 6.2]], [['2019-03-10 0:00AM' , 6.2]]

I found this code but unfortunately only for the comma.

var er = rep.replace(/\s*,\s*/g, " 0:00AM , ");

Answer Source

You need some regexp to make it work, please try this

your RegExp should look like this

var regexp = /(\'[a-zA-Z_0-9-]+)\'?/g;

so you should have a string like this:

var st = "[['2019-02-10', 6.2]],[['2019-03-10', 6.2]]";

then you can replace the desired content like this

st.replace(regexp, "$1 0:00AM'")

notice that $1 represent the first match regexp against the string

so finally you should have it like this

"[['2019-02-10 0:00AM', 6.2]],[['2019-03-10 0:00AM', 6.2]]"

I hope it can help you