Defineing dot function on a String to call several function

I wonder how I can define some dot function in js like .toupperCase that can call on a string or object without getting any parameters. For example I want to define a function on a string that calls a several function itself like

let ex = "hi .this is  &$234234 a test @#";
let tmp = ex.textformating();

String.prototype.textformating = function (){
    return this.replace(/[^a-z0-9 ]/gi, '  ').trim();
}

>Solution :

Your code works fine. You just have to define the function before you call it.

String.prototype.textformating = function (){
    return this.replace(/[^a-z0-9 ]/gi, '  ').trim();
}

let ex = "hi .this is  &$234234 a test @#";
let tmp = ex.textformating();

console.log('original:', ex)
console.log('formatted:', tmp)

Leave a Reply