I am trying to replace '&' and 'space' from a string.
I can remove the space by string.replace(/[\s]/g, '');
and remove the special character & by string.replace(/[^\da-zA-Z]/g, '')
Can I use both regex in one code? for removing special charecters and space from the string.
Use | to "or" regexes, e.g.
/(\s|&)/g
Grouping via (...) can be necessary to scope what gets or'd.
In this case, you just have two selectors, so it should work without as well.
/\s|&/g
Combine regex for & and space /[& ]+/g
var str='abzx12& 1'
console.log(str.replace(/[& ]+/g,''));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Here you go with one more solution
.replace(/ |&/g,'')
Example
var a = "asdasdas dsadasdas dasdas asdas & dasdasd &&&&";
console.log(a.replace(/ |&/g,''));
Try this if you don't want to use regex.
var str = "abc def & ghi jkl & mno";
console.log(str.split(' ').join('').split('&').join(''));
first replace space with null and than replace '&' with null.
It might be help you.
Related
I'd like to replace the "&" character, along with characters that may interfere with urls syntax.
so far i tried:
myText = myText.replace(/[^a-zA-Z0-9-. ]/g,'');
that probably works for other characters (didn't test it) but didn't comprehend the "&" which is what i care most about, so i added in combo the following line but also didn't get rid of the &:
myText = myText.replace(/&/g,'');
but neither work, how can i replace this special character?
SOLUTION:
Code was reading & at delivery and not &, so i had to do:
myText = myText.replace(/&/g,'');
and it works.
SNIPPET:
var text = "god & damn it";
console.log(text.replace(/&|&/g,''));
According to your comments, what you are trying to replace is this &, the html encoding of the & character.
With lodash you can _.unescape the string before replacing:
myText = _.unescape(myText).replace(/&/g, '');
This way you handle both & and & cases. Then if you have to append that text in the html you should _.escape it back to prevent weird side effects: _.escape(myText);.
Without lodash you can just search both in your regex:
myText = myText.replace(/&|&/g, '');
But this method can have it's side effects when other special characters are present because it removes the & character too, for example this string "Three is > than two & one" would end up looking like this "Three is gt; than two one" (notice the ugly gt; in the middle)
console.log("m&yText".replace(/\&/g,''))
I can suggest adding the backslash character before the & as to 'escape' using the & as the regex character. You want the regex to find and replace any literal & character.
I want to replace a text after a forward slash and before a end parantheses excluding the characters.
My text:
<h3>notThisText/IWantToReplaceThis)<h3>
$('h3').text($('h3').text().replace(regEx, 'textReplaced'));
Wanted result after replace:
notThisText/textReplaced)
I have tried
regex = /([^\/]+$)+/ //replaces the parantheses as well
regex = \/([^\)]+) //replaces the slash as well
but as you can see in my comments neither of these excludes both the slash and the end parantheses. Can someone help?
A pattern like /(?<=\/)[^)]+(?=\))/ won't work in JS as its regex engine does not support a lookbehind construct. So, you should use one of the following solutions:
s.replace(/(\/)[^)]+(\))/, '$1textReplaced$2')
s.replace(/(\/)[^)]+(?=\))/, '$1textReplaced')
s.replace(/(\/)[^)]+/, '$1textReplaced')
s.replace(/\/[^)]+\)/, '/textReplaced)')
The (...) forms a capturing group that can be referenced to with $ + number, a backreference, from the replacement pattern. The first solution is consuming / and ), and puts them into capturing groups. If you need to match consecutive, overlapping matches, use the second solution (s.replace(/(\/)[^)]+(?=\))/, '$1textReplaced')). If the ) is not required at the end, the third solution (replace(/(\/)[^)]+/, '$1textReplaced')) will do. The last solution (s.replace(/\/[^)]+\)/, '/textReplaced)')) will work if the / and ) are static values known beforehand.
You can use str.split('/')
var text = 'notThisText/IWantToReplaceThis';
var splited = text.split('/');
splited[1] = 'yourDesireText';
var output = splited.join('/');
console.log(output);
Try Following: In your case startChar='/', endChar = ')', origString=$('h3').text()
function customReplace(startChar, endChar, origString, replaceWith){
var strArray = origString.split(startChar);
return strArray[0] + startChar + replaceWith + endChar;
}
First of all, you didn't define clearly what is the format of the text which you want to replace and the non-replacement part. For example,
Does notThisText contain any slash /?
Does IWantToReplaceThis contain any parentheses )?
Since there are too many uncertainties, the answer here only shows up the pattern exactly matches your example:
yourText.replace(/(\/).*?(\))/g, '$1textReplaced$2')
var text = "notThisText/IWantToReplaceThis";
text = text.replace(/\/.*/, "/whatever");
output : "notThisText/whatever"`
I have a text for example as below:
"head1>data1,data2,data3|head2>data1,data2,data3|head3>data3,data4,data5**
now I want to replace ">data1..|" with "|"
I am using this: ".replace(/>\S+\||>\S+$/g,"|");"
But this is not helping as it gives me data as below:
"head1|head3|" instead of "head1|head2|head3|"
I am unable to find the right method.
You can use
>\S+?(?:\||$)
See the regex demo
The point is to make \S+ lazy, and to shorten the pattern we can use place the >\S+? before the alternation group.
Pattern details:
>\S+? - a literal > followed with 1+ non-whitespace symbols but as few as possible up to
(?:\||$) - a literal | or the end of string.
A simple approach :), was trying like this
var str = "head1>data1,data2,data3|head2>data1,data2,data3|head3>data3,data4,data5";
console.log(str.replace(/>[a-z1-9,]+/g,"|").replace(/\|+/g, "|"));
>[a-z1-9,]+ will select >data1,data2,data3
and then replaced multiple | with single |
:)
You can use:
>[a-z0-9,]+\|
and then replace this with single | every time.
I am trying to create a regular expression for the string filtering. I want to get the symbol "#" and anything that is written after that and before a space.
Can someone help me with this?
For example:
hi I am #vaibhav .
The expected result this regular expression should give is vaibhav.
I made this:
/#[a-z]*/
However, I am not sure if this will confirm to the above mentioned criteria.
To get a substring from the # up to the first space after it, use
#\S+
See demo
The \S means a non-whitespace character.
If you do not need #, use a capturing group:
#(\S+)
The value you need will be in Group 1. See another demo.
If you are using JavaScript:
var re = /#(\S+)/g;
var str = 'hi I am #vaibhav . hi, and I am #strib .';
var m;
while ((m = re.exec(str)) !== null) {
document.write("The value is: <b>" + m[1] + "</b><br/>");
}
The simplest solution is to use a negated set.
Search characters that are not '#'
Read in the '#'
Now capture characters that are not ' '
If you're trying to match and capture you can accomplish that like this:
[^#]*#([^ ]*).*
[Live Example]
If you only want to search then you don't need to match the whole string and you can just extract the actual match section:
#([^ ]*)
[Live Example]
The most complicated situation is where you need to deal with an escaped '#'. Here's an example of a match using that:
(?:[^\\#]|\\.)*#([^ ]*).*
[Live Example]
You can do that with lookarounds.
Edited version:
(?<=#)\w+
Demo on regex101
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/