Jump to content

why the method replace replace only the fisrt match?


Nati323
 Share

Recommended Posts

hello , i did the next code:

 

var s = "hello hello hello hello";document.write(s.replace("hello", "bye"));

and the result :

bye hellp hello hello

 

why is that? i want that the all "hello" will be replaced, do i have to use rexexp (/hello/g) ?

Edited by Nati323
Link to comment
Share on other sites

That's the default behavior. You can either specify some additional flags to make the replace global, or its more popular to use a regular expression with the global flag.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

i did :

 

 

var txt = "hello hello hello hello";document.write(txt.replace("hello", "bye", "g")); 

dosent work, (IE 11)

Edited by Nati323
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...