Regex for both newline and backslash for Replace function - javascript

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, '');

Related

JS\TS - unable to add a white space in a string based on multiple special characters

I'm looking for a smart way to add white space after special character in a long string.
let str = "this\is\an\example\for\a\long\string";
str = str.split("\\").join("\\ ");
This would produce:
"this\ is\ an\ example\ for\ a\ long\ string";
I am looking for something more generic to capture multiple special chars at once, something like this:
let str = "this.is.a\long-mixed.string\with\many.special/characters";
str = str.split(/[.\-_]/).join(/[. \- _ ]/); //note the white spaces after the dot, hyphen and slash. I need to cover as much special chars as possible.
EDIT
I need this to support multi languages. So basically English\Arabic\Hebrew words should not be whitespaced, But only insert a whitespace after a special char.
You can do it like this
So here with replace i am matching anything except alphabets and digits. and than simply adding a space to it.
let str = "this.is.a\long-mixed.string\with\many.special/characters";
str = str.replace(/([\W_])/g, "$1 ");
console.log(str);
([\W_]) - Matches anything except alphabets and digits.

Javascript | Can't replace \n with String.replace()

I have code which parse web-site and take information from database.
It's look like this:
var find = body.match(/\"text\":\"(.*?)\",\"date\"/);
As result, I have:
гороскоп на июль скорпион\nштукатурка на газобетон\nподработка на день\nмицубиси тюмень\nсокращение микрорайон
Then i try to replace \n, but it's don't working.
var str = find[1].replace(new RegExp("\\n",'g'),"*");
What I can do with this?
It looks like you want to replace the text \n, i.e. a backslash followed by an n, as opposed to a newline character.
In which case you can try
var str = find[1].replace(/\\n/g, "*");
or the less readable version
var str = find[1].replace(new RegExp("\\\\n", "g"), "*");
In regular expressions, the string \n matches a newline character. To match a backslash character we need to 'escape' it, by preceding it with another backslash. \\ in a regular expression matches a \ character. Similarly, in JavaScript string literals, \ is the escape character, so we need to escape both backslashes in the regular expression again when we write new RegExp("\\\\n", "g").
Working in the console!
Here this works globally and works on both types of line breaks:
find[1].replace(/\r?\n/g, "*")
if you dont want the '\r' to be replaced you could simply remove that from the regex.
removes all 3 types of line breaks
let s = find[1].replace(/(\r\n|\n|\r)/gm, " - ")

regexp problem, the dot selects all text

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.

javascript : how to replace symbol with regex?

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"

Is there a JavaScript regular expression to remove all whitespace except newline?

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, '')

Categories

Resources