Javascript regex - remove all special characters except semi colon - javascript

In javascript, how do I remove all special characters from the string except the semi-colon?
sample string: ABC/D A.b.c.;Qwerty
should return: ABCDAbc;Qwerty

You can use a regex that removes anything that isn't an alpha character or a semicolon like this /[^A-Za-z;]/g.
const str = "ABC/D A.b.c.;Qwerty";
const result = str.replace(/[^A-Za-z;]/g, "");
console.log(result);

var str = "ABC/D A.b.c.;Qwerty";
var result = str.replace(/[^A-Za-z;]/g, "");​​ // 21ABCDAbc;Qwerty
Live DEMO

Related

Remove part of the string before the FIRST dot with js

I have the next problem. I need to remove a part of the string before the first dot in it. I've tried to use split function:
var str = "P001.M003.PO888393";
str = str.split(".").pop();
But the result of str is "PO888393".
I need to remove only the part before the first dot. I want next result: "M003.PO888393".
Someone knows how can I do this? Thanks!
One solution that I can come up with is finding the index of the first period and then extracting the rest of the string from that index+1 using the substring method.
let str = "P001.M003.PO888393";
str = str.substring(str.indexOf('.')+1);
console.log(str)
You can use split and splice function to remove the first entry and use join function to merge the other two strings again as follows:
str = str.split('.').splice(1).join('.');
Result is
M003.PO888393
var str = "P001.M003.PO888393";
str = str.split('.').splice(1).join('.');
console.log(str);
You could use a regular expression with .replace() to match everything from the start of your string up until the first dot ., and replace that with an empty string.
var str = "P001.M003.PO888393";
var res = str.replace(/^[^\.]*\./, '');
console.log(res);
Regex explanation:
^ Match the beginning of the string
[^\.]* match zero or more (*) characters that are not a . character.
\. match a . character
Using these combined matches the first characters in the string include the first ., and replaces it with an empty string ''.
calling replace on the string with regex /^\w+\./g will do it:
let re = /^\w+\./g
let result = "P001.M003.PO888393".replace(re,'')
console.log(result)
where:
\w is word character
+ means one or more times
\. literally .
many way to achieve that:
by using slice function:
let str = "P001.M003.PO888393";
str = str.slice(str.indexOf('.') + 1);
by using substring function
let str = "P001.M003.PO888393";
str = str.substring(str.indexOf('.') + 1);
by using substr function
let str = "P001.M003.PO888393";
str = str.substr(str.indexOf('.') + 1);
and ...

Replace function in Js to replace periods and comma into one hypen

I need help in javascript where the word entered can be replaced as:
Input - A.. BC
Output - A-BC
the code that i have tried is:
var text = 'A.. BC';
new_text = text.replace(' ', '-') && text.replace('.','-');
console.log(new_text);
This is not working as it is giving me the output as:
A-. BC
I'd use a regular expression instead. Use a character set to match one or more dots, commas, or spaces, then replace with a dash:
const change = str => str.replace(/[., ]+/g, '-');
console.log(change('A.. BC'));
Use a charater set
var text = 'A.. BC';
new_text = text.replace(/[., ]+/g, '-');
console.log(new_text);
you can try replacing all non-alphabetical characters with a hyphen with regex:
const a = 'A.. BC';
const b = 'A ..BC';
// Find all non-word characters regex
const r = /[\W]+/g;
const hyphen = '-';
void console.log(a.replace(r, hyphen));
void console.log(b.replace(r, hyphen));
// A-BC
// A-BC

Remove string between special character JavaScript

I trying to remove some string from string which I have.
I have following string.
"[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#"
I want to remove text between ^ # including that character.
Output should be "[1fe3-46675-be1a-cd97084b] dsd dsds [4j34-46675-be1a-cd97854b]"
I used following but, not removing that string.
let str = "[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#"
str = str.replace(/^.*#/g, '');
console.log(str);
You can do it with this regex.
let stringsS = "[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#"
let regex = /\^(.*?)\#/gi
console.log(stringsS.replace(regex,''));
Try this:
str = "[1fe3-46675-be1a-cd97084b]^Some Text# dsd dsds [4j34-46675-be1a-cd97854b]^Another Text#";
replacedStr = str.replace(/\^[^#]*\#/g, '');
console.log(replacedStr)
Escape the ^ as in:
str = str.replace(/\^.*#/, '');
The ^ means something in regex and must be escaped if you want it to be treated as normal character

Need to replace a string from a long string in javascript

I have a long string
Full_str1 = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;';
removable_str2 = 'ab#xyz.com;';
I need to have a replaced string which will have
resultant Final string should look like,
cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;
I tried with
str3 = Full_str1.replace(new RegExp('(^|\\b)' +removable_str2, 'g'),"");
but it resulted in
cab#xyz.com;c-c.c_ab#xyz.com;
Here a soluce using two separated regex for each case :
the str to remove is at the start of the string
the str to remove is inside or at the end of the string
PS :
I couldn't perform it in one regex, because it would remove an extra ; in case of matching the string to remove inside of the global string.
const originalStr = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;ab#xyz.com;c_ab#xyz.com;';
const toRemove = 'ab#xyz.com;';
const epuredStr = originalStr
.replace(new RegExp(`^${toRemove}`, 'g'), '')
.replace(new RegExp(`;${toRemove}`, 'g'), ';');
console.log(epuredStr);
First, the dynamic part must be escaped, else, . will match any char but a line break char, and will match ab#xyz§com;, too.
Next, you need to match this only at the start of the string or after ;. So, you may use
var Full_str1 = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;';
var removable_str2 = 'ab#xyz.com;';
var rx = new RegExp("(^|;)" + removable_str2.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), "g");
console.log(Full_str1.replace(rx, "$1"));
// => cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;
Replace "g" with "gi" for case insensitive matching.
See the regex demo. Note that (^|;) matches and captures into Group 1 start of string location (empty string) or ; and $1 in the replacement pattern restores this char in the result.
NOTE: If the pattern is known beforehand and you only want to handle ab#xyz.com; pattern, use a regex literal without escaping, Full_str1.replace(/(^|;)ab#xyz\.com;/g, "$1").
i don't find any particular description why you haven't tried like this it will give you desired result cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;
const full_str1 = 'ab#xyz.com;cab#xyz.com;c-ab#xyz.com;c.ab#xyz.com;c_ab#xyz.com;';
const removable_str2 = 'ab#xyz.com;';
const result= full_str1.replace(removable_str2 , "");
console.log(result);

Replace a substring with javascript

Need to replace a substring in URL (technically just a string) with javascript.
The string like
http://blah-blah.com/search?par_one=test&par_two=anothertest&SearchableText=TO_REPLACE
or
http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest
means, the word to replace can be either at the most end of the URL or in the middle of it.
I am trying to cover these with the following:
var newWord = NEW_SEARCH_TERM;
var str = 'http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest';
var regex = /^\S+SearchableText=(.*)&?\S*$/;
str = str.replace(regex, newWord);
But no matter what I do I get str = NEW_SEARCH_TERM. Moreover the regular expression when I try it in RegExhibit, selects the word to replace and everything that follows it that is not what I want.
How can I write a universal expression to cover both cases and make the correct string be saved in the variable?
str.replace(/SearchableText=[^&]*/, 'SearchableText=' + newWord)
The \S+ and \S* in your regex match all non-whitespace characters.
You probably want to remove them and the anchors.
http://jsfiddle.net/mplungjan/ZGbsY/
ClyFish did it while I was fiddling
var url1="http://blah-blah.com/search?par_one=test&par_two=anothertest&SearchableText=TO_REPLACE";
var url2 ="http://blah-blah.com/search?par_one=test&SearchableText=TO_REPLACE&par_two=anothertest"
var newWord = "foo";
function replaceSearch(str,newWord) {
var regex = /SearchableText=[^&]*/;
return str.replace(regex, "SearchableText="+newWord);
}
document.write(replaceSearch(url1,newWord))
document.write('<hr>');
document.write(replaceSearch(url2,newWord))

Categories