I've a strings like pie
and cherrypie
.
How do I replace all the occurrences of pie
in the strings, that aren't started with pie
?
Here's what I've at the moment:
var regexp = new RegExp('pie', 'g');
var string = 'pie';
var string2 = 'cherrypie';
var cap = function(val) {
return val.charAt(0).toUpperCase()+val.slice(1);
}
console.log(string.replace(regexp, cap));
console.log(string2.replace(regexp, cap));
Online demo - http://jsbin.com/ERUpuYuq/1/
Just open a console and you will see two lines:
Pie
cherryPie
An expected result is
pie - since it starts with "pie"
cherryPie
I've tried to add *
symbol at the beginning of my regexp, without any luck.
If I set .pie
as my regexp, I get this:
pie
cherrYpie
The solution would be a regexp replacement, that will catch all the occurances, that are placed not at the beginnign of the string.
Anyone knows regexp?
Note: cap
function shouldn't be modified.