hidValue="javaScript:java";
replaceStr = "java";
resultStr=hidValue.replace("/\b"+replaceStr+"\b/gi","");
resultStr still contains "javaScript:java"
The above code is not replacing the exact string java. But when I change the code and directly pass the value 'java' it's getting replaced correctly i.e
hidValue="javaScript:java";
resultStr=hidValue.replace(/\bjava\b/gi,"");
resultStr contains "javaScript:"
So how should I pass a variable to replace function such that only the exact match is replaced.
The replace-function does not take a string as first argument but a RegExp-object. You may not mix those two up. To create a RexExp-object out of a combined string, use the appropriate constructor:
resultStr=hidValue.replace(new RegExp("\\b"+replaceStr+"\\b","gi"),"");
Note the double backslashes: You want a backslash in your Regular Expression, but a backslash also serves as escape character in the string, so you'll have to double it.
Notice that in one case you're passing a regular expression literal /\bjava\b/gi, and in the other you're passing a string "/\bjava\b/gi". When using a string as the pattern, String.replace will look for that string, it will not treat the pattern as a regular expression.
If you need to make a regular expression using variables, do it like so:
new RegExp("\\b" + replaceStr + "\\b", "gi")
See:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
`let msisdn = '5093240556699'
let isdnWith = numb.msisdn.slice(8,11);
let msisdnNew = msisdn.replace(isdnWith, 'XXX', 'gi');
show 5093240556XXX`
Related
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 textArea. I am trying to split each string from a paragraph, which has proper grammar based punctuation delimiters like ,.!? or more if any.
I am trying to achieve this using Javascript. I am trying to get all such strings in that using the regular expression as in this answer
But here, in javascript for me it's not working. Here's my code snippet for more clarity
$('#split').click(function(){
var textAreaContent = $('#textArea').val();
//split the string i.e.., textArea content
var splittedArray = textAreaContent.split("\\W+");
alert("Splitted Array is "+splittedArray);
var lengthOfsplittedArray = splittedArray.length;
alert('lengthOfText '+lengthOfsplittedArray);
});
Since its unable to split, its always showing length as 1. What could be the apt regular expression here.
The regular expression shouldn't differ between Java and JavaScript, but the .split() method in Java accepts a regular expression string. If you want to use a regular expression in JavaScript, you need to create one...like so:
.split(/\W+/)
DEMO: http://jsfiddle.net/s3B5J/
Notice the / and / to create a regular expression literal. The Java version needed two "\" because it was enclosed in a string.
Reference:
https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
You can try this
textAreaContent.split(/\W+/);
\W+ : Matches any character that is not a word character (alphanumeric & underscore).
so it counts except alphanumerics and underscore! if you dont need to split " " (space) then you can use;
var splittedArray = textAreaContent.split("/\n+/");
I've seen plenty of regex examples that will not allow any special characters. I need one that requires at least one special character.
I'm looking at a C# regex
var regexItem = new Regex("^[a-zA-Z0-9 ]*$");
Can this be converted to use with javascript? Do I need to escape any of the characters?
Based an example I have built this so far:
var regex = "^[a-zA-Z0-9 ]*$";
//Must have one special character
if (regex.exec(resetPassword)) {
isValid = false;
$('#vsResetPassword').append('Password must contain at least 1 special character.');
}
Can someone please identify my error, or guide me down a more efficient path? The error I'm currently getting is that regex has no 'exec' method
Your problem is that "^[a-zA-Z0-9 ]*$" is a string, and you need a regex:
var regex = /^[a-zA-Z0-9 ]*$/; // one way
var regex = new RegExp("^[a-zA-Z0-9 ]*$"); // another way
[more information]
Other than that, your code looks fine.
In javascript, regexs are formatted like this:
/^[a-zA-Z0-9 ]*$/
Note that there are no quotation marks and instead you use forward slashes at the beginning and end.
In javascript, you can create a regular expression object two ways.
1) You can use the constructor method with the RegExp object (note the different spelling than what you were using):
var regexItem = new RegExp("^[a-zA-Z0-9 ]*$");
2) You can use the literal syntax built into the language:
var regexItem = /^[a-zA-Z0-9 ]*$/;
The advantage of the second is that you only have to escape a forward slash, you don't have to worry about quotes. The advantage of the first is that you can programmatically construct a string from various parts and then pass it to the RegExp constructor.
Further, the optional flags for the regular expression are passed like this in the two forms:
var regexItem = new RegExp("^[A-Z0-9 ]*$", "i");
var regexItem = /^[A-Z0-9 ]*$/i;
In javascript, it seems to be a more common convention to the user /regex/ method that is built into the parser unless you are dynamically constructing a string or the flags.
I have the following snippet. I want to find the appearance of a, but it does not work. How can I put the variable right?
var string1 = 'asdgghjajakhakhdsadsafdgawerwweadf';
var string2 = 'a';
string1.match('/' + string2 + '/g').length;
You need to use the RegExp constructor instead of a regex literal.
var string = 'asdgghjjkhkh';
var string2 = 'a';
var regex = new RegExp( string2, 'g' );
string.match(regex);
If you didn't need the global modifier, then you could just pass string2, and .match() will create the regex for you.
string.match( string2 );
If you are merely looking to check whether a string contains another string, then your best bet is simply to use match() without a regex.
You may object: But I need a regex to check for classes, like \s, to define complicated patterns, etc..
In that case: You will need change the syntax even more, double-escaping your classes and dropping starting/ending / regex indicator symbols.
Imagine this regex...
someString.match(/\bcool|tubular\b);
The exact equivalent of this, when using a new new RegExp(), is...
someStringRegex = new RegExp('\\bcool|tubular\\b');
Two things happened in this transition:
Drop the opening and closing / (otherwise, your regex will fail).
Double escape your character classes, like \b becomes \\b for word borders, and \w becomes \\w for whitespace, etc. (otherwise, your regex will fail).
Here is another example-
//confirm whether a string contains target at its end (both are variables in the function below, e.g. confirm whether str "Abstraction" contains target "action" at the end).
function confirmEnding(string, target) {
let regex = new RegExp(target);
return regex.test(string);
};
I have a working reg expression that does a replace function based on the expression. It works perfect. It finds a specific string based on the beginning of the string and the expression. This is it:
str.replace(/\bevent[0-9]*\=/, "event");
what this does is it changes event=1 to event.
What if event was a variable word? What if I needed to look for conference also?
I have tried:
var type = "conference";
str.replace(/\b/ + type+ /[0-9]*\=/, "conference");
and:
str.replace(/\b/type/[0-9]*\=/, "conference");
neither worked.
how can I pass a javascript string into a regular expression?
Instead of writing a RegEx literal, use a string to create a new RegExp object:
str.replace(new RegExp('\b' + var + '[0-9]'), …)
You can do that with the RegExp Object:
str.replace(RegExp('\b' + reStr + '[0-9]*\='),StrToReplaceWith)
Create a new regex object with your variable.
read this...
http://www.w3schools.com/js/js_obj_regexp.asp