It's because this regex /(^|\s)a(\s|$)/g
match the previous char and the next char to each a
in string "a a a a"
the regex matches :
Edit: Little bit tricky but working (without regex):
var a = "a a a a";
// Handle beginning case 'a '
var startI = a.indexOf("a ");
if (startI === 0){
var off = a.charAt(startI + 2) !== "a" ? 2 : 1; // test if "a" come next to keep the space before
a = a.slice(startI + off);
}
// Handle middle case ' a '
var iOf = -1;
while ((iOf = a.indexOf(" a ")) > -1){
var off = a.charAt(iOf + 3) !== "a" ? 3 : 2; // same here
a = a.slice(0, iOf) + a.slice(iOf+off, a.length);
}
// Handle end case ' a'
var endI = a.indexOf(" a");
if (endI === a.length - 2){
a = a.slice(0, endI);
}
a; // ""
(^|\s)a(?=\s|$)
Try this.Replace by $1
.See demo.
https://regex101.com/r/gQ3kS4/3
Use this instead:
"a a a a".replace(/(^|\s*)a(\s|$)/g, '$1')
With "* this you replace all the "a" occurrences
Greetings
Here is the problem, I need to create an image file with a .svg file. I have a function which should draw the svg, and then, i get it from a canvas to save it as an image file. my draw function is : ...
Here is the problem, I need to create an image file with a .svg file. I have a function which should draw the svg, and then, i get it from a canvas to save it as an image file. my draw function is : ...
I have an iframe that adjusts it's size dynamical. I solved this with a post message and a listener on the parent page, so everytime the content of the iframe changes the iframe size will too and ...
I have an iframe that adjusts it's size dynamical. I solved this with a post message and a listener on the parent page, so everytime the content of the iframe changes the iframe size will too and ...
is there a way to create a breeze predicate for a property that its type is Edm.Decimal? because the datatype in next expression is always double and I don't find a way to say to breeze that I just ...
is there a way to create a breeze predicate for a property that its type is Edm.Decimal? because the datatype in next expression is always double and I don't find a way to say to breeze that I just ...
I've tried to follow default Web API tutorial: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api Here's what I did: 1) I added Action Routing in my ...
I've tried to follow default Web API tutorial: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api Here's what I did: 1) I added Action Routing in my ...