JavaScript JS string replace only replacing every other occurence string replace only first instance c#,string replace only first

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 :

  • "a " , then the string to check become "a a a"$ (but now the start of the string is not the beginning and there is not space before)
  • " a " (the third a) , then become "a"$ (that not match because no space before)

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; // ""
Answer:1
(^|\s)a(?=\s|$)

Try this.Replace by $1.See demo.

https://regex101.com/r/gQ3kS4/3

Answer:2

Use this instead:

"a a a a".replace(/(^|\s*)a(\s|$)/g, '$1')

With "* this you replace all the "a" occurrences

Greetings

Answer:3

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 : ...

  1. image onload firing
  2. image.onload not working
  3. image onload not working chrome
  4. image onload not working ie
  5. image onload doesn't work
  6. image onload javascript not working
  7. image onload function not working javascript
  8. image onload function not working
  9. jquery image onload not working
  10. canvas image onload not working
  11. firefox image onload not working
  12. react image onload not working
  13. safari image onload not working
  14. js image onload not working
  15. javascript image onload not working ie

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 ...

  1. trigger function when div is visible
  2. trigger function when option is selected
  3. trigger function when checkbox is checked
  4. trigger function when div is loaded
  5. trigger function on page load
  6. trigger function on scroll javascript
  7. trigger function on checkbox click
  8. trigger function on click
  9. trigger function on oscilloscope
  10. trigger function on click jquery
  11. trigger function on enter key
  12. trigger function on window resize
  13. trigger function on select change
  14. trigger function on scroll jquery
  15. trigger function on state change react
  16. trigger function on click javascript
  17. trigger function on page load jquery
  18. trigger function on modal close
  19. trigger function on button click
  20. trigger function on load

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 ...