Gurbaz Pooran
Gurbaz Pooran

Reputation: 41

Javascript: add a character to a string containing a specific word

If I have the following string: table.row.columns.values.many. I am looking to add character * after values. So expected output would be: table.row.columns.values*.many . If there is no "values" in string then string should stay the same.

Upvotes: 0

Views: 453

Answers (2)

Ghost Ops
Ghost Ops

Reputation: 1734

I think this works nice for you, really simple, and easy way

var str = "table.row.columns.values.many";
str = str.replace("values", "values*")

console.log(str)

Upvotes: 0

Kirill Savik
Kirill Savik

Reputation: 1278

Please use this code.

let str = "table.row.columns.values.many"
str = str.replace("values", "values*");
console.log(str);

You can use also "replaceAll" if you want to replace all "values".

Upvotes: 1

Related Questions