I have a string as str = "aa#gmail.com,pp#gmailcom,cc#gmail.com,"
I am replacing the string with quotes like below
var re = new RegExp("cc#gmail.com", "g");
$('reminder_email').value= str.replace(re," ");
I am able to replace the email id that matches in regex.. But the comma still remains there. So the result will be like
"aa#gmail.com,pp#gmailcom,,"
But I need to replace the email id with comma (which comes after each one).. How to do that..
try this
demo
str = "aa#gmail.com,pp#gmailcom,cc#gmail.com,"
var re = new RegExp("cc#gmail.com,", "g");
a= str.replace(re," ");
The problem is you don't know if you'll leave a dangling leading, dangling trailing or doubled comma depending on whether the removed target is first, last or in the middle respectively.
The easy way is to clean up misplaced commas after doing the first replace:
str.replace(re," ").replace(/,,+/, ",").replace(/^,|,$/, "");
Related
I Need to replace a set of characters from a string, I don't have control over the string so I can't just escape the + symbol inside the string.
So my question is, seeing as this works if I change my value to 'breeding' it does replace the string. How can I escape a string without manually escaping them? I have tried
var s = "http://example.co/kb/tags/anazolic~racing~all+articles~breeding";
var value = 'all+articles';
var find = new RegExp('\~?\\b' + value + '\\b', 'g');
var l = s.replace(find, '');
console.log(l);
DEMO: http://jsfiddle.net/AnBc6/1/
I have also tried adding: value = encodeURIComponent(value); but this didn't work either.
Any Help?
So, if I understand correctly, you want to escape special regex characters.
value = value.replace(/[-\\()\[\]{}^$*+.?|]/g, '\\$&');
You could extract this to a function of course:
function escapeRegex(value) {
return String(value).replace(/[-\\()\[\]{}^$*+.?|]/g, '\\$&');
}
Change the third line to this:
var find = new RegExp('\~?\\b' + value.replace(/\+/g,'\\+') + '\\b', 'g');
The plus sign is a special character in a Regular Expression, so it needs to be escaped with a backslash.
(Also, I'm not sure what you mean by "stored in a variable." Everything in JavaScript is "in a variable." Or maybe you really mean, "stored in a RegExp object.")
I have the below String value to be displayed in text area and i want to remove the first characters ##*n|n from the string .
The string is as follows :
Symbol-001
##*n|nClaimant Name
##*n|nTransaction
I have used the below code to deal with removing the special characters
var paramVal1 = parent.noteText; //paramVal1 will have the string now
var pattern = /[##*n|n]/g;
var paramVal1 = paramVal1.replace(pattern,'');
document.getElementById("txtNoteArea").value = paramval1;//appending the refined string to text area
For the above used code am getting the out put string as below
Symbol-001
|Claimat Name //here 'n' is missing and i have an extra '|' character
|Transactio //'n' is missing here too and an extra '|' character
Kindly help to remove the characters ##*n|n without affecting the other values
What your regex is saying is "remove any of the following characters: #|*n". Clearly this isn't what you want!
Try this instead: /##\*n\|n/g
This says "remove the literal string ##*n|n". The backslashes remove the special meaning from * and |.
You are using regular expression reserved chars in your pattern, you need to escape them
You can use this expression:
var pattern = /[\#\#\*n\|n]/g;
i think use this /[##*n\|n]/g regEx
If you want to replace the first occurrence as you say on your question, you don't need to use regex. A simple string will do, as long as you escape the asterisk:
var str = "Symbol-001 ##*n|nClaimant Name ##*n|nTransaction";
var str2 = str.replace("##\*n|n", ""); //output: "Symbol-001 Claimant Name ##*n|nTransaction"
If you want to replace all the occurrences, you can use regex, escaping all the characters that have a special meaning:
var str3 = str.replace(/\#\#\*n\|n/g, ""); //output: "Symbol-001 Claimant Name Transaction"
Have a look at this regex builder, might come in handy - http://gskinner.com/RegExr/
I have a string and I need to fix it in order to append it to a query.
Say I have the string "A Basket For Every Occasion" and I want it to be "A-Basket-For-Every-Occasion"
I need to find a space and replace it with a hyphen. Then, I need to check if there is another space in the string. If not, return the fixed string. If so, run the same process again.
Sounds like a recursive function to me but I am not sure how to set it up. Any help would be greatly appreciated.
You can use a regex replacement like this:
var str = "A Basket For Every Occasion";
str = str.replace(/\s/g, "-");
The "g" flag in the regex will cause all spaces to get replaced.
You may want to collapse multiple spaces to a single hyphen so you don't end up with multiple dashes in a row. That would look like this:
var str = "A Basket For Every Occasion";
str = str.replace(/\s+/g, "-");
Use replace and find for whitespaces \s globally (flag g)
var a = "asd asd sad".replace(/\s/g,"-");
a becomes
"asd-asd-sad"
Try
value = value.split(' ').join('-');
I used this to get rid of my spaces. Instead of the hyphen I made it empty and works great. Also it is all JS. .split(limiter) will delete the limiter and puts the string pieces in an array (with no limiter elements) then you can join the array with the hyphens.
Okay I have a simple Javascript problem, and I hope some of you are eager to help me. I realize it's not very difficult but I've been working whole day and just can't get my head around it.
Here it goes: I have a sentence in a Textfield form and I need to reprint the content of a sentence but WITHOUT spaces.
For example: "My name is Slavisha" The result: "MynameisSlavisha"
Thank you
You can replace all whitespace characters:
var str = "My name is Slavisha" ;
str = str.replace(/\s+/g, ""); // "MynameisSlavisha"
The /\s+/g regex will match any whitespace character, the g flag is necessary to replace all the occurrences on your string.
Also, as you can see, we need to reassign the str variable because Strings are immutable -they can't really change-.
Another way to do it:
var str = 'My name is Slavisha'.split(' ').join('');
I'm trying to search for '[EN]' in the string 'Nationality [EN] [ESP]', I want to remove this from the string so I'm using a replace method, code examaple below
var str = 'Nationality [EN] [ESP]';
var find = "[EN]";
var regex = new RegExp(find, "g");
alert(str.replace(regex, ''));
Since [EN] is identified as a character set this will output the string 'Nationality [] [ESP]' but I want to remove the square brackets aswell. I thought that I could escape them using \ but it didn't work
Any advice would be much appreciated
Try setting your regex this way:
var regex = /\[EN\]/g;
If you just want to replace a single instance of it you can just str = str.replace("[EN] ", ""); otherwise; var find = "\\[EN\\]";.