Escape RegExp.lastMatch ($&) in a string - javascript

I have an input box which the user types text into. I'm using the inputted text and replacing a portion of a large text file.
The problem is, if the user inputs the $& characters it will result in a lastMatch instead of the literal text which is of course a dollar sign ($) followed by an ampersand (&) without any special meaning.
You can see the user input here:
To simulate the user input, I wrote the following code:
var originalString = "# Mandatory parameter\n#EPCDatabase/EPCdatabase.param/epc.db.user=\n# Mandatory parameter";
var regexExpression = new RegExp('#EPCDatabase\/EPCdatabase.param\/epc.db.user=.*$', "im");
var replaceSting = "EPCDatabase\/EPCdatabase.param\/epc.db.user=test#$#%^%>$&<%(*"
var newPropertiesText = originalString.replace(regexExpression, replaceSting);
console.log(originalString);
console.log(newPropertiesText);
The problem is that instead of appending the $& literally, it will append the lastMatch, and the newPropertiesText created as follows:
As explained here, I've tried escaping the $ with two $$, so it will mean a literal dollar instead of the special combination:
"EPCDatabase\/EPCdatabase.param\/epc.db.user=aaaa#$#%^%>$&<%(*".replace(/\$&/g, '$$&');
here is the problem ^^
But that didn't help, so I tried different combination of \\$\\$ and similar patterns. But I couldn't send to the regex a simple $& literal.
EDIT:
Using this escape function didn't solve the issue:
RegExp.escape= function(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
var originalString = "# Mandatory parameter\n#EPCDatabase/EPCdatabase.param/epc.db.user=\n# Mandatory parameter";
var regexExpression = new RegExp('#EPCDatabase\/EPCdatabase.param\/epc.db.user=.*$', "im");
var replaceSting = RegExp.escape("EPCDatabase\/EPCdatabase.param\/epc.db.user=aaaa#$#%^%>$&<%(*");
var newPropertiesText = originalString.replace(regexExpression, replaceSting);
console.log(originalString);
console.log(newPropertiesText);
You can see it still returns the lastMatch.

So, after additional reading, thinking, some tries and additional #FFFFFF hairs, I got what it takes to solve this issue.
In order to tell the regex to put a literal $& instead of the entire match, you'll have to use the following:
.replace(/\$&/, '$$$$&')
$$ tells the regex engine one literal dollar sign, $$$$ tells it two literal dollar signs -> and two literal dollar signs one after the other in the replacement string actually become one literal dollar sign $.
Followed by an & its become a normal literal $& without last match meaning.
var originalString = "# Mandatory parameter\n#EPCDatabase/EPCdatabase.param/epc.db.user=\n# Mandatory parameter";
var regexExpression = new RegExp('#EPCDatabase\/EPCdatabase.param\/epc.db.user=.*$', "im");
var replaceSting = "EPCDatabase\/EPCdatabase.param\/epc.db.user=aaaa#$#%^%>$&<%(*".replace(/\$&/, '$$$$&');
var newPropertiesText = originalString.replace(regexExpression, replaceSting);
console.log(originalString);
console.log(newPropertiesText);
You can view the code here if you want to play with it.

Related

Get replaced characters with javascript regex replace

I am currently replacing all non-letter characters using
var stringwithoutspecialCharacter = "testwordwithpunctiuation.".replace(/[^\w\s!?]/g, '');
The problem is that I do not know which special character will appear (that needs removing). However I do need to be able to access the removed special character after I've run some code with the word without the special character.
Example inputs:
"test".
(temporary)
foo,
Desired output:
['"','test','"',"."]
['(','temporary',')']
['foo',',']
How could this be achieved in javascript?
Edit: To get both valid and invalid characters, change the regular expression
Quick solution is to define an array to collect the matches.
Then pass in a function into your replace() call
var matches = [];
var matcher = function(match, offset, string) {
matches.push(match);
return '';
}
var stringwithoutspecialCharacter = "testwordwithpunctiuation.".replace(/[^\w\s!?]|[\w\s!?]+/g, matcher);
console.log("Matches: " + matches);

Splitting a string at special character with JavaScript

I am trying to "intelligently" pre-fill a form, I want to prefill the firstname and lastname inputs based on a user email address, so for example,
jon.doe#email.com RETURNS Jon Doe
jon_doe#email.com RETURN Jon Doe
jon-doe#email.com RETURNS Jon Doe
I have managed to get the string before the #,
var email = letters.substr(0, letters.indexOf('#'));
But cant work out how to split() when the separator can be multiple values, I can do this,
email.split("_")
but how can I split on other email address valid special characters?
JavaScript's string split method can take a regex.
For example the following will split on ., -, and _.
"i-am_john.doe".split(/[.\-_]/)
Returning the following.
["i", "am", "john", "doe"]
You can use a regular expression for what you want to split on. You can for example split on anything that isn't a letter:
var parts = email.split(/[^A-Za-z]/);
Demo: http://jsfiddle.net/Guffa/xt3Lb9e6/
You can split a string using a regular expression. To match ., _ or -, you can use a character class, for example [.\-_]. The syntax for regular expressions in JavaScript is /expression/, so your example would look like:
email.split(/[\.\-_]/);
Note that the backslashes are to prevent . and - being interpreted as special characters. . is a special character class representing any character. In a character class, - can be used to specify ranges, such as [a-z].
If you require a dynamic list of characters to split on, you can build a regular expression using the RegExp constructor. For example:
var specialChars = ['.', '\\-', '_'];
var specialRegex = new RegExp('[' + specialChars.join('') + ']');
email.split(specialRegex);
More information on regular expressions in JavaScript can be found on MDN.
Regular Expressions --
email.split(/[_\.-]/)
This one matches (therefore splits at) any of (a character set, indicated by []) _, ., or -.
Here's a good resource for learning regular expressions: http://qntm.org/files/re/re.html
You can use regex to do it, just provide a list of the characters in square brackets and escape if necessary.
email.split("[_-\.]");
Is that what you mean?
You are correct that you need to use the split function.
Split function works by taking an argument to split the string on. Multiple values can be split via regular expression. For you usage, try something like
var re = /[\._\-]/;
var split = email.split(re, 2);
This should result in an array with two values, first/second name. The second argument is the number of elements returned.
I created a jsFiddle to show how this could be done :
function printName(email){
var name = email.split('#')[0];
// source : http://stackoverflow.com/questions/650022/how-do-i-split-a-string-with-multiple-separators-in-javascript
var returnVal = name.split(/[._-]/g);
return returnVal;
}
http://jsfiddle.net/ts6nx9tt/1/
If you define your seperators, below code can return all alternatives for you.
var arr = ["_",".","-"];
var email = letters.substr(0, letters.indexOf('#'));
arr.map(function(val,index,rest){
var r = email.split(val);
if(r.length > 1){
return r.join(' ');
}
return "";
}
);

How to remove the special characters from a string using javascript

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/

Javascript match last #<User>

I'm trying to make an auto-complete function for twitter usernames.
So far, I have the following code:
function OnKeyUp(txtboxid){
var text = $('#'+txtboxid).val()
var regex = '(^|\s)#(\w*[a-zA-Z_]+\w*)'
var results = text.match(RegExp(regex, 'gm'))
console.debug(results)
}
The problem is, it matches only text when it is at the beginning of the string (eg: #yser)
What i want is a regex that can mach such a string like this "hello #user2 , #user and #user3 how are you"
I'm not sure how to accomplish this.
Searched google for about 3 hours now and still nothing found.
Also, it would be great to only the the last username when its changed.
Your regex is fine. The only problem is that backslashes in the string will be removed or replaced when the string is parsed, instead of being interpreted by the regular expression parser. You need to re-escape each of them with an extra backslash:
var regex = '(^|\\s)#(\\w*[a-zA-Z_]+\\w*)';
Instead of specifying the regular expression with a string and the RegEx function, you should usually use a regular expression literal. It's delimited by backslashes instead of double-quotes, with the flags appended to the end:
var results = text.match(/(^|\s)#(\w*[a-zA-Z_]+\w*)/gm);

How to find regexp in a string and attach it to a variable

Let's say we have a string in JavaScript "This is a nice website - http://stackoverflow.com". I want to extract the URL along with the three preceding characters (space dash space) using RegExp and attach the extracted string to a variable.
var string = "This is a nice website - http://stackoverflow.com";
var reg = ""; //no idea how to write this regexp for extracting url and three preceding chars
// and after some magic I would get
var extracedString = " - http://www.stackoverflow.com";
Anyone? Thanks.
var extractedString = string.replace(/^.*(...http:.+)$/, "$1");
if (extractedString == string) {
alert("No match");
}
The dot . matches every character, so three dots match three arbitrary characters. The ^ and $ match start and end of the string.
Note, that this won't work for
more than one URL
HTTPS, mailto, FTP, SSH, ... (although you can simply expand it, like this: (https?|ftp|ssh))

Categories