JavaScript Simple RegEx wierd result

I am trying to replace all occurences of 'S' (capital S) with '.sss' :

For example the following line doesn't even contain a capital S in it and it still replaces a lot :

 "MM/dd/yyyy EEEE hh:mm aa".replace(new RegExp('S*', 'g'),'.sss')

The result is :

".sssM.sssM.sss/.sssd.sssd.sss/.sssy.sssy.sssy.sssy.sss .sssE.sssE.sssE.sssE.sss .sssh.sssh.sss:.sssm.sssm.sss .sssa.sssa.sss"

Don't have any Ideea ..

(to test this example just copy and paste it in chrome or firefox dev tools on any page and run it)

Answer:1

That is because * matches 0 or more, so you are also matching the occurence of 0 "S", means it matches every position, where "S" is not ahead, or behind (see it on Regexr). Change it to

S+

see it on Regexr, or even only "S".

+ Quantifier matches one or more.

Answer:2

Use RegExp('S+', 'g')

because * matches zero or more times the previous character.

"+" matches the character at least 1 time but also more.

Answer:3
var res = str.replace(/S/g,".sss"); 

Try this. Use this link

Answer:4

I am pushing HTML Code including <script> in a div, I am able to push inside the div successfully using innerHTML but script is not executing. Code: $.ajax({ data: '', dataType: 'html'...

I am pushing HTML Code including <script> in a div, I am able to push inside the div successfully using innerHTML but script is not executing. Code: $.ajax({ data: '', dataType: 'html'...

  1. delete script after execution
  2. script execution timed out after 30000ms
  3. remove script after execution
  4. exit shell script after execution
  5. pause powershell script after execution
  6. exit bash script after execution
  7. javascript remove script after execution
  8. powershell delete script after execution

Lets say I have some constructors: function Foo() { } function FooObject() { return {} } function FooArray() { return [] } function FooFunction() { return function () {} } function FooString() { ...

Lets say I have some constructors: function Foo() { } function FooObject() { return {} } function FooArray() { return [] } function FooFunction() { return function () {} } function FooString() { ...

  1. canvas
  2. canada
  3. cancelled
  4. canceled vs cancelled
  5. canada vaccines
  6. canada goose
  7. cane corso
  8. canes
  9. canadian prime minister
  10. cancun flights
  11. canker sore
  12. candy
  13. cancer
  14. cancun
  15. canon
  16. canvas prints
  17. cancer sign
  18. candace owens
  19. canada news
  20. candytopia

I have two states in my current apps, which is login and register. If I just open my website towards localhost:8001, it is automatically redirect to /login because I've set $urlRouterProvider....

I have two states in my current apps, which is login and register. If I just open my website towards localhost:8001, it is automatically redirect to /login because I've set $urlRouterProvider....

  1. angularjs route reload
  2. angularjs router reload page
  3. angularjs routing reload page
  4. angularjs reload routing
  5. angularjs ui-router reload current state
  6. angularjs ui router reload
  7. angularjs ui router reload parent state
  8. angularjs ui router reload page
  9. angularjs ui router reload state
  10. angularjs router force reload
  11. angularjs ui router reload controller
  12. angularjs ui-router force reload

I'm trying to make a box follow the cursor on mousemove. However, it should stop following the mouse if the user clicks the box the user's mouse leaves the surrounding div Here's what the current ...

I'm trying to make a box follow the cursor on mousemove. However, it should stop following the mouse if the user clicks the box the user's mouse leaves the surrounding div Here's what the current ...