How to remove special characters like $, @, % from string in jquery
You should explore Regex.
Try this:
var str = 'The student have 100% of attendance in @school';str= str.replace(/[^\w\s]/gi, '')document.write(str);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
How to trim special characters from start & end of the html input using jQuery or JavaScript?
You can use the regex below to achieve your result:
^[-.,:'_/]+|[-.,:'_/]+$
Explanation of the above Regex:
^
- Represents the start of the test String.
|
- Represents alternation.
$
- Represents end of the given test string.
[-.,:'_/]+
- Start or end of the string contains one or more of the characters mentioned.
Demo of the above regex here.
Implementation in JAVASCRIPT(JQUERY). You can modify the code accordingly.
const regex = /^[-.,:'_/]+|[-.,:'-_/]+$/gm;
const str = `bsvisbvskbvksv
nvlvnlvlv slcls
vnelvnelvnelv vnvsvnlvnl!
ocnsocnsocnosc ohoiIBIBiiwciv!
:nvkvnskvbskv
-bvksvbskvbsk sncksncks -cnscns-
--bvsjvbjvbjbvdbvkdbvkd-- vvnskvnskd --
lsvnslvnlsvnls:
Hello world
hellow WOLDF`;
//Notice in the 6th case above; only the last "-" gets replaced
const subst = ``;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
JQuery - Slice/Remove - Special characters from ID or class name
Using pop
and split
you can get new id without (スーペリアルーム) and set new id to div like below.
var id = $('div').attr('id');var arr = id.split("-");//remove last element from arrayvar popItem = arr.pop();//change array back to string and replace , with -var result = arr.toString().replace(',','-');//Give New ID for div$('div').attr('id',result)console.log($('div').attr('id'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id =superior-room-(スーペリアルーム)" class="list-item"> <p>Title</p></div>
How to remove special characters from URL in jQuery?
Look into
var sPageUrl = "http://localhost/DoSomething/Index/123#";
var number = sPageUrl.substring(sPageUrl.lastIndexOf('/') + 1).replace(/[^\w\s]/gi, '');
alert(number);
You could use regex to strip all special characters from the string
Remove all special characters except space from a string using JavaScript
You should use the string replace function, with a single regex.
Assuming by special characters, you mean anything that's not letter, here is a solution:
const str = "abc's test#s";console.log(str.replace(/[^a-zA-Z ]/g, ""));
Replace the special character and space from the string in javascript
Simply try using
/[\W_]/g
\W
match any non-word character[^a-zA-Z0-9_]
Included _
if you also want to remove it then
Regex
Related Topics
How to Make Some Text from a Json String Bold
Laravel Pdf File Download from Ajax Request (Laravel 5)
Converting Async/Await Function to Normal Es5 Targetting Ie 11
How to Let a User Download Multiple Files When a Button Is Clicked
Why Is It a Bad Practice to Return Generated HTML Instead of Json or Is It
Javascript, Track Iframes Redirecting Top Window
Domexception: Play() Can Only Be Initiated by a User Gesture
How to Style Dynamically Created Elements With CSS
How to Add Counter in Angular 6
How to Conditionally Include Script Element in Angular
Will a React Component Re-Render If Its Props Are Updated, But Don't Change in Value
Regexp to Validate a Non-Zero Number
How to Show Div When a Radio Button Is Clicked in React
Jquery Ajax Readystate 0 Responsetext Status 0 Statustext Error
Get Selected Value from Multiple Select on Change in Dynamic Form