Javascript Jquery - how to replace a word but only when not part of another word

0 votes

I am currently doing a regex comparison to remove words (rude words) from a text field when written by the user. At the moment it performs the check when the user hits space and removes the word if matches. However it will remove the word even if it is part of another word. So if you type apple followed by space it will be removed, that's ok. But if you type applepie followed by space it will remove 'apple' and leave pie, that's not ok. I am trying to make it so that in this instance if apple is part of another word it will not be removed.

Is there any way I can perform the comparison on the whole word only or ignore the comparison if it is combined with other characters?

I know that this allows people to write many rude things with no space. But that is the desired effect by the people that give me orders :(

Thanks for any help.

function rude(string) {

var regex = /apple|pear|orange|banana/ig; 
//exaple words because I'm sure you don't need to read profanity 

var updatedString = string.replace( regex, function(s) {
    var blank = "";
    return blank;
});
return updatedString;
}

$(input).keyup(function(event) {
   var text;
   if (event.keyCode == 32) {
       var text = rude($(this).val());
       $(this).val(text);
       $("someText").html(text);
   }
}

Aug 22, 2022 in Web Development by gaurav
• 23,260 points
693 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Web Development

0 votes
1 answer

How to create a simple map using JavaScript/JQuery

var map = new Object(); // or ...READ MORE

answered Jun 27, 2022 in Web Development by rajatha
• 7,680 points
1,368 views
0 votes
0 answers

how to get values of columns for a selected row through jQuery

here i am trying to fetch values ...READ MORE

Jul 1, 2022 in Web Development by gaurav
• 23,260 points
5,448 views
0 votes
0 answers

How to scroll to top of page with JavaScript/jQuery?

Is there a way to control browser ...READ MORE

Jul 1, 2022 in Web Development by gaurav
• 23,260 points
2,280 views
0 votes
0 answers

How to get the value of a checkbox flipswitch in JQuery Mobile 1.4.5?

I'm using following markup to create a ...READ MORE

Aug 22, 2022 in Web Development by gaurav
• 23,260 points
548 views
0 votes
0 answers

How can I select only visible elements within a specific section of my page using jQuery selectors?

How can I select only visible elements ...READ MORE

5 days ago in Web Development by Nidhi
• 3,820 points
22 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,520 points
1,347 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
15,871 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
23,376 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,840 points
3,339 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP