Remove all special characters from string in JS - javascript

I want to remove all special characters from string and add only one "-"(hyphen) in the place.
Consider below example
var string = 'Lorem%^$&*&^Ipsum#^is#!^&simply!dummy text.'
So, from the above string, if there is a continuous number of special characters then I want to remove all of them and add only one "-" or if there is a single or double special character then also that should be replaced by "-"
Result should be like this
Lorem-Ipsum-is-simply-dummy text-
I have tried below, but no luck
var newString = sourceString.replace(/[\. ,:-]+/g, "-");

You could use .replace to replace all non-alphabetical character substrings with -:
const input = 'Lorem%^$&*&^Ipsum#^is#!^&simply!dummy text.';
const output = input.replace(/[^\w\s]+/gi, '-');
console.log(output);
If you want to permit numbers too:
const input = 'Lorem123%^$&*&^654Ipsum#^is#!^&simply!dummy text.';
const output = input.replace(/[^\w\s\d]+/gi, '-');
console.log(output);

Related

How to regex replace a query string with matching 2 words?

I have a url and I want to replace the query string. For example
www.test.com/is/images/383773?wid=200&hei=200
I want to match the wid= and hei= and the numbers don't have to be 200 to replace the whole thing so it should look like this.
Expected
www.test.com/is/images/383773?#HT_dtImage
So I've tried doing but it only replaced the matching wei and hei.
const url = "www.test.com/is/images/383773?wid=200&hei=200"
url.replace(/(wid)(hei)_[^\&]+/, "#HT_dtImage")
You can match either wid= or hei= until the next optional ampersand and then remove those matches, and then append #HT_dtImage to the result.
\b(?:wid|hei)=[^&]*&?
The pattern matches:
\b A word boundary to prevent a partial word match
(?:wid|hei)= Non capture group, match either wid or hei followed by =
[^&]*&? Match 0+ times a char other than &, and then match an optional &
See a regex demo.
let url = "www.test.com/is/images/383773?wid=200&hei=200"
url = url.replace(/\b(?:wid|hei)=[^&]*&?/g, "") + "#HT_dtImage";
console.log(url)
I would just use string split here:
var url = "www.test.com/is/images/383773?wid=200&hei=200";
var output = url.split("?")[0] + "?#HT_dtImage";
console.log(output);
If you only want to target query strings havings both keys wid and hei, then use a regex approach:
var url = "www.test.com/is/images/383773?wid=200&hei=200";
var output = url.replace(/(.*)\?(?=.*\bwid=\d+)(?=.*\bhei=\d+).*/, "$1?#HT_dtImage");
console.log(output);
You can make use of lookaround using regex /\?.*/
const url = 'www.test.com/is/images/383773?wid=200&hei=200';
const result = url.replace(/\?.*/, '?#HT_dtImage');
console.log(result);
Try
url.replace(/\?.*/, "?#HT_dtImage")

Replace Regex Symbols + Blank Space

There is any way to make a regex to replace symbols + blank space?
Im using:
const cleanMask = (value) => {
const output = value.replace(/[_()-]/g, "").trim();
return output;
}
let result = cleanMask('this (contains parens) and_underscore, and-dash')
console.log(result)
Its it right?
Your current code will replace all occurrences of characters _, (, ) and - with an empty string and then trim() whitespace from the beginning and end of the result.
If you want to remove ALL whitespace, you can use the whitespace character class \s instead of trim() like this:
const output = value.replace(/[_()-\s]/g, "");

Is it possible to do a double replace?

If double replace is it possible to do?
var string = [link="<iframe"qwe"></iframe>"]
var output = string.replace(/[link="([^"]+)"]/g, '$1.replace(/"([^"]+)"/g, "'")');
what i want output:
[link="<iframe'qwe'></iframe>"]
You can use a function as the replacement in replace(). It can then do its own replace on the capture group.
var string = '[link="<iframe"qwe"></iframe>"]';
var output = string.replace(/link="([^\]]+)"]/g, (match, group1) =>
'link="' + group1.replace(/"/g, "'") + '"]');
console.log(output);
Note also that I had to correct your regexp. ([^"]+) should be ([^\]]+) so that it can match a string containing double quotes -- you need to capture that so you can replace the double quotes.
And in the second replacement, you want to match ", not [^"]+

How to replace numbers with an empty char

i need to replace phone number in string on \n new line.
My string: Jhony Jhons,jhon#gmail.com,380967574366
I tried this:
var str = 'Jhony Jhons,jhon#gmail.com,380967574366'
var regex = /[0-9]/g;
var rec = str.trim().replace(regex, '\n').split(','); //Jhony Jhons,jhon#gmail.com,
Number replace on \n but after using e-mail extra comma is in the string need to remove it.
Finally my string should look like this:
Jhony Jhons,jhon#gmail.com\n
You can try this:
var str = 'Jhony Jhons,jhon#gmail.com,380967574366';
var regex = /,[0-9]+/g;
str.replace(regex, '\n');
The snippet above may output what you want, i.e. Jhony Jhons,jhon#gmail.com\n
There's a lot of ways to that, and this is so easy, so try this simple answer:-
var str = 'Jhony Jhons,jhon#gmail.com,380967574366';
var splitted = str.split(","); //split them by comma
splitted.pop(); //removes the last element
var rec = splitted.join() + '\n'; //join them
You need a regex to select the complete phone number and also the preceding comma. Your current regex selects each digit and replaces each one with an "\n", resulting in a lot of "\n" in the result. Also the regex does not match the comma.
Use the following regex:
var str = 'Jhony Jhons,jhon#gmail.com,380967574366'
var regex = /,[0-9]+$/;
// it replaces all consecutive digits with the condition at least one digit exists (the "[0-9]+" part)
// placed at the end of the string (the "$" part)
// and also the digits must be preceded by a comma (the "," part in the beginning);
// also no need for global flag (/g) because of the $ symbol (the end of the string) which can be matched only once
var rec = str.trim().replace(regex, '\n'); //the result will be this string: Jhony Jhons,jhon#gmail.com\n
var str = "Jhony Jhons,jhon#gmail.com,380967574366";
var result = str.replace(/,\d+/g,'\\n');
console.log(result)

JS - Split string into substrings by regex

Let's say I have a string that starts by 7878 and ends by 0d0a or 0D0A such as:
var string = "78780d0101234567890123450016efe20d0a";
var string2 = "78780d0101234567890123450016efe20d0a78780d0103588990504943870016efe20d0a";
var string 3 = "78780d0101234567890123450016efe20d0a78780d0103588990504943870016efe20d0a78780d0101234567890123450016efe20d0a"
How can I split it by regex so it becomes an array like:
['78780d0101234567890123450016efe20d0a']
['78780d0101234567890123450016efe20d0a','78780d0101234567890123450016efe20d0a']
['78780d0101234567890123450016efe20d0a','78780d0101234567890123450016efe20d0a','78780d0101234567890123450016efe20d0a']
You can split the string with a positive lookahead (?=7878). The regex isn't consuming any characters, so 7878 will be part of the string.
var rgx = /(?=7878)/;
console.log(string1.split(rgx));
console.log(string2.split(rgx));
console.log(string3.split(rgx));
Another option is to split on '7878' and then take all the elements except first and add '7878' to each of them. For example:
var arr = string3.split('7878').slice(1).map(function(str){
return '7878' + str;
});
That works BUT it also matches strings that do NOT end on 0d0a. How
can I only matches those ending on 0d0a OR 0D0A?
Well, then you can use String.match with a plain regex.
console.log(string3.match(/7878.*?0d0a/ig));

Categories

Resources