how to remove the symbols such as ▼, >>,<< and others using the regex in javascript?
You can use the replace function for this, specifying the empty string as the replacement string. Here are a couple examples.
If you only want to strip specific characters:
s = s.replace(/[▼><]/g, '');
Or using a Unicode escape sequence:
s = s.replace(/[\u25bc><]/g, '');
If you want to strip all but alphanumeric characters:
s = s.replace(/[^A-Za-z0-9]/, '');
Edit: described Unicode escape sequence usage.
I'd remove non-standard character(s) by using the unicode token \u and the corresponding character code.
For example:
// Remove "▼" using its character code
var s = "I like milk ▼.".replace(/\u9660/g, "");
You can use replace(/[\u0100-\uffff]/g, '') to remove characters outside the extended ASCII range.
E.g.
>>> "I 𝔻Ȯ𝓝ʼṮ like ȖŋŀℭỚ𝕯Ễ Regexs‽‽‽".replace(/[\u0080-\uffff]/g, '')
"I like Regexs"
Related
I have some price tags like $23.47,-$50.00, $0.37, $113.57,$600,0456.00 and so on. I intend to remove dollar($),thousand separator(,) and also any white space.
For this purpose I am using regex as
(Sometext).replace(/^[, ]+|[, ]+$|[, ]+/g, "").replace('$',"").replace(/\s+/g, ' ');
Is there any better way to combine all this operations(remove thousand separator comma, remove dollar symbol, remove white space) with single replace?
Can you just use the regex OR operator? Something like this:
(Sometext).replace(/^[, ]+|[, ]+$|[, ]+|\$|\s+/g, "");
The | represent an OR in regex. I just placed the pipe between the three patterns you want to match, and used them within a single replace(). I also escaped the $ in dollar sign matching statement.
Based on this this answer try:
var price = "$23 . 47"
var simpleValue = price.match(/(\d+)/g).join("")
You can use something like
'$444,55,22.33'.replace(/[,\s$]/g, "");
You could target anything NOT what you want to keep.
So, a regex like: price.replace(/[^\d\.-]/g, '');
The ^ in the brackets tells it to match anything but the included characters.
The \d allows any numeric character.
I am using a replace function to escape some characters (both newline and backslash) from a string.
Here is my code:
var str = strElement.replace(/\\/\n/g, "");
I am trying to use regex, so that I can add more special characters if needed. Is this a valid regex or can someone tell me what am I doing wrong here?
You're ending the regex early with an unescaped forward slash. You also want to use a set to match individual characters. Additionally you might want to add "\r" (carriage return) in as well as "\n" (new line).
This should work:
var str = strElement.replace(/[\\\n\r]/g, "");
This is not a valid regex as the slash is a delimiter and ends the regex. What you probably wanted is the pipe (|), which is an alternation:
var str = strElement.replace(/\\|\n/g, "");
In case you need to extend it in the future it may be helpful to use a character class to improve readability:
var str = strElement.replace(/[\\\nabcx]/g, "");
A character class matches a single character from it's body.
This should work. The regular expression replaces both the newline characters and the backslashes in escaped html text:
var str = strElement.replace(/\\n|\\r|\\/g, '');
I want to replace all symbols that aren't letters by -, but my code doesn't work :
$reg = '/[^a-zA-Z]+/g';
$txt = $txt.replace($reg, '-');
What am I doing wrong?
Regular Expressions in JavaScript are not strings.
reg = /[^a-z]+/gi;
txt = txt.replace(reg, '-');
You don't need to place quotes around them.
You need to un-quote the regex string so it's treated as a regular expression literal, so you get this:
$reg = /[^a-zA-Z]+/g;
$txt = $txt.replace($reg, '-');
Regular expressions in JavaScript don't need to be quoted as strings unless using the new Regexp() notation; in the above example, it is now a regular expression literal, which isn't treated as a string but a piece of regex to be used in .replace().
do not use quote on regex. Without quotes, they are RegEx object. With quotes they are just string.
Use,
$reg = /[^a-zA-Z]+/g;
Remove the quotes from around your regex.
If it is your intention for multiple non-alpha characters in a row to be replaced with a single hyphen your regex will then work. If you want multiple non-alpha characters to be replaced with multiple hyphens then you should also remove the + sign.
I use some jquery to highlight search results. For some reason if i enter a basis dot, all of the text get selected. I use regex and replace to wrap the results in a tag to give the found matches a color.
the code that i use
var pattern = new.RegExp('('+$.unique(text.split(" ")).join("|")+")","gi");
how can i prevent that the dot selects all text, so i want to leave the point out of the code(the dot has no power)
You may be able to get there by doing this:
var pattern = new.RegExp('('+$.unique(text.replace('.', '\\.').split(" ")).join("|")+")","gi");
The idea here is that you're attempting to escape the period, which acts as a wild card in regex.
This will replace all special RegExp characters (except for | since you're using that to join the terms) with their escaped version so you won't get unwanted matches or syntax errors:
var str = $.unique(text.split(" ")).join("|"),
pattern;
str = str.replace(/[\\\.\+\*\?\^\$\[\]\(\)\{\}\/\'\#\:\!\=]/ig, "\\$&");
pattern = new RegExp('('+str+')', 'gi');
The dot is supposed to match all text (almost everything, really). If you want to match a period, you can just escape it as \..
If you have a period in your RegExp it's supposed to match any character besides newline characters. If you don't want that functionality you need to escape the period.
Example RegExp with period escaped /word\./
You need to escape the text you're putting into the regex, so that special characters don't have unwanted meanings. My code is based on some from phpjs.org:
var words = $.unique(text.split(" ")).join("|");
words = words.replace(/[.\\+*?\[\^\]$(){}=!<>|:\\-]/h, '\\$&'); // escape regex special chars
var pattern = new RegExp('(' + words + ")","gi");
This escapes the following characters: .\+*?[^]$(){}=!<>|:- with a backslash \ so you can safely insert them into your new RegExp construction.
How do I remove white spaces in a string but not new line character in JavaScript. I found a solution for C# , by using \t , but it's not supported in JavaScript.
To make it more clear, here's an example:
var s = "this\n is a\n te st"
using regexp method I expect it to return
"this\nisa\ntest"
[^\S\r\n]+
Not a non-whitespace char, not \r and not \n; one or more instances.
This will work, even on \t.
var newstr = s.replace(/ +?/g, '');
Although in Javascript / /g does match \t, I find it can hide the original intent as it reads as a match for the space character. The alternative would be to use a character collection explicitly listing the whitespace characters, excluding \n. i.e. /[ \t\r]+/g.
var newString = s.replace(/[ \t\r]+/g,"");
If you want to match every whitespace character that \s matches except for newlines, you could use this:
/[\t\v\f\r \u00a0\u2000-\u200b\u2028-\u2029\u3000]+/g
Note that this will remove carriage returns (\r), so if the input contains \r\n pairs, they will be converted to just \n. If you want to preserve carriage returns, just remove the \r from the regular expression.
Try this
var trimmedString = orgString.replace(/^\s+|\s+$/g, '') ;
This does the trick:
str.replace(/ /g, "")
and the space does NOT match tabs or linebreaks (CHROME45), no plus or questionmark is needed when replacing globally.
In Perl you have the "horizontal whitespace" shorthand \h to destinguish between linebreaks and spaces but unfortunately not in JavaScript.
The \t shorthand on the other hand IS supported in JavaScript, but it describes the tabulator only.
const str = "abc def ghi";
str.replace(/\s/g, "")
-> "abcdefghi"
try this '/^\\s*/'
code.replace(/^\s[^\S]*/gm, '')
works for me on text like:
#set($todayString = $util.time.nowEpochMilliSeconds())
#set($pk = $util.autoId())
$util.qr($ctx.stash.put("postId", $pk))
and removes the space/tabs before the first 3 lines with removing the spaces in the line.
*optimisation by #Toto:
code.replace(/^\s+/gm, '')