Regular expression works on browser but not in Node.js - javascript

I have to match some strings from the given variables. Say I have variable
var x = 'elseif testing';
Now I want get value "testing" from this string. So wrote.
x.match(/^elseif(.*)/);
Late I realized sometimes I do get string:
var x = 'else if testing';
So I wrote expression to match:
x.match(/^else[\s+]if(.*)/);
This works well on browser but not in Node.js. Any reason why ?

Try without double escapes and character class:
x.match(/^else\s*if(.*)/i);
Also added i for case insensitive search.
Also note [\s+] will also match a literal + since inside character class + (and many other regex special characters) is considered a literal plus.

If you're explicitly wanting to match only one or zero spaces, then:
x.match(/^else\s?if(.*)/i);

Related

Replace word and characters in string

I'm in the midst of figuring out the RegExp function capabilities and trying to create this string:
api/seaarch/redirect/complete/127879/2013-11-27/4-12-2013/7/2/0/0/LGW/null
from this:
/api/search/redirect/complete/127879/2013-11-27/4-12-2013/7/2/0/0/undefined/undefined/undefined/undefined/undefined/undefined/undefined/undefined/undefined/^^undefined^undefined^undefined^undefined^undefined/undefined^undefined^undefined^undefined^undefined^undefined^undefined/undefined^undefined^undefined^undefined^undefined^undefined^undefined/undefined^undefined^undefined^undefined^undefined^undefined^undefined/LGW/null
I know \bundefined\b\^ removes 'undefined^' and undefined\b\/ removes 'undefined/' but how do i combine these together?
In this case, since the ^ or / follow the undefined in the same place, you can use a character class:
str = str.replace(/\bundefined[/^]+/g, '');
Note: ^ is special both inside and outside of character classes, but in different ways. Inside a character class ([...]), it's only special if it's the first character, hence my not making it the first char above.
Also note the + at the end, saying "one or more" of the ^ or /. Without that, since there are a couple of double ^^, you end up with ^ in the result.
If you want to be a bit paranoid (and I admit I probably would be), you could escape the / within the character class. For me it works if you don't, with Chrome's implementation, but trying to follow the pattern definition in the spec is...tiresome...so I honestly don't know if there's any implementation out there that would try to end the expression as of the / in the character class. So with the escape:
str = str.replace(/\bundefined[\/^]+/g, '');

Find Value in CSV with Regex

I am trying to determine if a CSV string contains a specific number (also String), in this case the number 3. I have wrote some script to attempt this but the result always returns null. The regex works when using an online testing tool, however not when utilized via script. Can anyone determine what I'm missing?
Here is my code:
var csv = ["1,25,3","3", "1", "1,9,10", "2,4,5,6,7,11,33,3", "2,1,2,12,15,27"];
function contains(param){
var regex = /(,)?\D[3]\D(,)?/g;
return param.match(regex);
}
for(var i = 0; i < csv.length; i++){
console.log(contains(csv[i]));
}
Or if you prefer: JsFiddle
The problem is that your pattern requires a character (\D) before and after your 3. Since all 3s in your example are at the end of the string the second \D can never match. What you want is probably something like this:
var regex = /(?:^|\D)3(?!\d)/;
For the end of the string we use negative lookahead. That asserts that there is no digit. This is better than asserting that there is a non-digit character (because it works for the end of the string, too). Ideally, we would use the same for the beginning, but that is not supported by JavaScript. So we say, either we have the beginning of the string or a non-digit character. In fact (as Brad Koch pointed out), in this specific case, both conditions constitute a word boundary (a position between a character in [a-zA-Z0-9_] and one that is not or an end of the string). So you can simply use:
var regex = /\b3\b/;
However, if your input can include other characters than digits and commas (e.g. 1,text,2,a3b,somemoretext), none of these approaches are sufficient. Instead you need to check for commas explicitly:
var regex = /(?:^|,)3(?![^,])/;
Also, since you don't need the actual match, but only want to know whether there is a match, you can use test instead:
return regex.test(param);
This will give you a boolean instead of an array (which probably also makes is marginally more efficient).

Issue with custom javascript regex

I have a custom regular expression which I use to detect whole numbers, fractions and floats.
var regEx = new RegExp("^((^[1-9]|(0\.)|(\.))([0-9]+)?((\s|\.)[0-9]+(/[0-9])?)?)$");
var quantity = 'd';
var matched = quantity.match(regEx);
alert(matched);
​
(The code is also found here: http://jsfiddle.net/aNb3L/ .)
The problem is that for a single letter it matches, and I can't figure out why. But for more letters it fails(which is good).
Disclaimer: I am new to regular expressions, although in http://gskinner.com/RegExr/ it doesn't match a single letter
It's easier to use straight regular expression syntax:
var regEx = /^((^[1-9]|(0\.)|(\.))([0-9]+)?((\s|\.)[0-9]+(\/[0-9])?)?)$/;
When you use the RegExp constructor, you have to double-up on the backslashes. As it is, your code only has single backslashes, so the \. subexpressions are being treated as . — and that's how single non-digit characters are slipping through.
Thus yours would also work this way:
var regEx = new RegExp("^((^[1-9]|(0\\.)|(\\.))([0-9]+)?((\\s|\\.)[0-9]+(/[0-9])?)?)$");
This happens because the string syntax also uses backslash as a quoting mechanism. When your regular expression is first parsed as a string constant, those backslashes are stripped out if you don't double them. When the string is then passed to the regular expression parser, they're gone.
The only time you really need to use the RegExp constructor is when you're building up the regular expression dynamically or when it's delivered to your code via JSON or something.
Well, for a whole number this would be your regex:
/^(0|[1-9]\d*)$/
Then you have to account for the possibility of a float:
/^(0|[1-9]\d*)(.\d+)?$/
Then you have to account for the possibility of a fraction:
/^(0|[1-9]\d*)((.\d+)|(\/[1-9]\d*)?$/
To me this regex is much easier to read than your original, but it's up to you of course.

JS Regex with variables and possible illegal chars

I cannot get this regex string to work in Javascript:
var input = $("input").val();
var hi = "(?<=[^ ​ ‍])" + input + "(?=[$ ​ ‍])";
var reg = new RegExp(hi);
alert(reg);
The last line is not working, but it does work when the regex is valid. I put the variable into a second string for the full regex search before passing that one to the regex object. Why isn't this regex query valid? (In case you are wondering, the chars in the brackets are space, zwsp, nbsp, and zwj.)
JavaScript regular expressions do not support look-behind.
They do however, support look-ahead, so if you really need the functionality you can reverse the input and write the expression "backwards". If you want both look-ahead and look-behind at the same time, this gets a little complicated.
As you haven't revealed what you're actually trying to achieve, you may be able to avoid the zero-width matches and just use normal capture groups.

Brackets in Regular Expression

I'd like to compare 2 strings with each other, but I got a little problem with the Brackets.
The String I want to seek looks like this:
CAPPL:LOCAL.L_hk[1].vorlauftemp_soll
Quoting those to bracket is seemingly useless.
I tried it with this code
var regex = new RegExp("CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll","gi");
var value = "CAPPL:LOCAL.L_hk[1].vorlauftemp_soll";
regex.test(value);
Somebody who can help me??
It is useless because you're using string. You need to escape the backslashes as well:
var regex = new RegExp("CAPPL:LOCAL.L_hk\\[1\\].vorlauftemp_soll","gi");
Or use a regex literal:
var regex = /CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll/gi
Unknown escape characters are ignored in JavaScript, so "\[" results in the same string as "[".
In value, you have (1) instead of [1]. So if you expect the regular expression to match and it doesn't, it because of that.
Another problem is that you're using "" in your expression. In order to write regular expression in JavaScript, use /.../g instead of "...".
You may also want to escape the dot in your expression. . means "any character that is not a line break". You, on the other hand, wants the dot to be matched literally: \..
You are generating a regular expression (in which [ is a special character that can be escaped with \) using a string (in which \ is a special character).
var regex = /CAPPL:LOCAL.L_hk\[1\].vorlauftemp_soll/gi;

Categories

Resources