Today I Learned - Rocky Kev

TIL how to replace many characters in javascript

POSTED ON:

TAGS:

You can use the replace() method to swap one string pattern for another.

For example, to remove all the periods and replace them with dashes, you do this.

var result = str.replace(".", "-");

But what if you want to replace spaces, commas, and periods?

You can put it in a regex like this:

var result = str.replace(/[ ,.]/g, "");

via redneb on StackOverflow


Related TILs

Tagged:

TIL fancy methods to transform Javascript Objects

You can use Object.entries(), Object.keys(), Object.fromEntries()...

TIL How to steal localData using an XSS attack

But that's just a red flag that opens the door to bigger issues.

TIL Clear-Site-Data

The Clear-Site-Data header clears browsing data (cookies, storage, cache) associated with the requesting website. It allows web developers to have more control over the data stored by a client browser for their origins.