0

I'm trying to abbreviate a long list of .replace() sterilisation I'm applying to a string variable by writing my own jquery method.

jQuery.fn.ACsterilise = function() {
    var text = this.text;
    var sterilisedText = "foo test bar";
    sterilisedText = text.toUpperCase().replace(/\'/g,"").replace(/\,/g,"").replace(/\-/g," ");
return sterilisedText;
};

and then calling it

var stringToSterilise = "testi'ng fo'o st'ring bar"
var sterilisedString = stringToSterilise.ACsterilise();

But with no luck. I feel jquery is a bad choice as I'm working with a string not the DOM. Am new to javascript.

1

1 Answer 1

1

jQuery is not made for working on strings, but on the DOM. What do you want?

jQuery instances:

jQuery.fn.ACsterilise = function() {
    var text = this.text(); // get text content
    var sterilisedText = text.toUpperCase().replace(/'|,/g,"").replace(/-/g," ");
    return sterilisedText;
};
console.log($("<a>Some odd-lookin' html</a>").ACsterilise());

Strings:

String.prototype.ACsterilise = function() {
    var text = this; // current string object
    var sterilisedText = text.toUpperCase().replace(/'|,/g,"").replace(/-/g," ");
    return sterilisedText;
};
console.log("testi'ng fo'o st'ring bar".ACsterilise());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.