Equivalent regex pattern from c# to javascript - javascript

Convertion of the regular expression from C# to javascript.
C#
(?<![\\]);
Javascript
/(?<![\\]);/
While using Regex.split, the regular expression for C# works fine but in javascript 'Unexcpected Quantifier' error occurs.
string
"CN=s\,tttrrr,OU=OU1,DC=dom1,DC=local;CN=g\;hi\,klm,OU=OU1,DC=dom1,DC=local;CN=rrr\ttt,OU=OU1,DC=dom1,DC=local;CN=Vvvv,OU=OU1,DC=dom1,DC=local"
Result
CN=s\,tttrrr,OU=OU1,DC=dom1,DC=local
CN=g\;hi\,klm,OU=OU1,DC=dom1,DC=local
CN=rrr\ttt,OU=OU1,DC=dom1,DC=local
CN=Vvvv,OU=OU1,DC=dom1,DC=local

Splitting the input based on ; which was preceded by a word boundary will give you the desired output.
> var str = "CN=s\\,tttrrr,OU=OU1,DC=dom1,DC=local;CN=g\\;hi\\,klm,OU=OU1,DC=dom1,DC=local;CN=rrr\ttt,OU=OU1,DC=dom1,DC=local;CN=Vvvv,OU=OU1,DC=dom1,DC=local"
undefined
> str.split(/\b;/g)
[ 'CN=s\\,tttrrr,OU=OU1,DC=dom1,DC=local',
'CN=g\\;hi\\,klm,OU=OU1,DC=dom1,DC=local',
'CN=rrr\ttt,OU=OU1,DC=dom1,DC=local',
'CN=Vvvv,OU=OU1,DC=dom1,DC=local' ]
DEMO

Related

Split keyword from javascript to C#

How can I split this JavaScript:
encodeUri(this.value).split("/%..|./").length - 1;
to C#? Should I use regex for this? I'm trying the snippet below to c# but error on the "/%..|./". It says that cannot convert from string to char.
HttpUtility.Html.Encode(value).Split(#"/%..|./").Length - 1;
There is no overload of the method String.Split that takes only one string as an input.
You probably want something like this:
HttpUtility.HtmlEncode(value).Split(new [] {"/%..|./"}, StringSplitOptions.None);
// or even:
HttpUtility.HtmlEncode(value).Split("/%..|./", StringSplitOptions.None);

Rewriting javascript regex in php when regex have escapes

I'm trying to write my regex as string (it's part of my S-Expression tokenizer that first split on string, regular expressions and lisp comments and then tokenize stuff between), it works in https://regex101.com/r/nH4kN6/1/ but have problem to write it as string for php.
My JavaScript regex look like this:
var pre_parse_re = /("(?:\\[\S\s]|[^"])*"|\/(?! )[^\/\\]*(?:\\[\S\s][^\/\\]*)*\/[gimy]*(?=\s|\(|\)|$)|;.*)/g;
I've tried to write this regex in php (the one from Regex101 was inside single quote).
$pre_parse_re = "%(\"(?:\\[\\S\\s]|[^\"])*\"|/(?! )[^/\\]*(?:\\[\\S\\s][^/\\]*)*/[gimy]*(?=\\s|\\(|\\)|$)|;.*)%";
My input
'(";()" /;;;/g baz); (baz quux)'
when called:
$parts = preg_split($pre_parse_re, $str, -1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
it should create same array as in Regex101 (3 matches and stuff between) but it keep splitting on first semicolon inside regex /;;;/g
I think your escaping might be incorrect. Try this regex instead:
$pre_parse_re = "%(\"(?:\\\\[\\\\S\\\\s]|[^\"])*\"|\/(?! )[^\/\\\\]*(?:\\\\[\S\s][^\/\\\\]*)*\/[gimy]*(?=\s|\(|\)|$)|;.*)%";
Using preg_split might also return more than the capturing groups that you want, so you could also change to use this if you just want the 3 matches.
$parts;
preg_match_all($pre_parse_re, $str, $parts, PREG_SET_ORDER, 0);

Passing regular expression from C# to Javascript not working

In my code I am passing the following regex from VB.net to javascript for Post Box address validation.
I am passing the following regex from VB.net
^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)
It works good in javascript. But its not case sensitive, it fails when we enter capital letters.
I tried the following regex for case insensitive, but it fails completely.
^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)/i
My requirement is that I need to pass the RegEx from VB.net to javascript.
Any help?
Passing /i would only work if you were using the literal syntax:
var rex = /^X$/i;
As you are calling the RegExp constructor you need to provide the i option as an argument:
var rex = new RegExp("^X$", "i");

replace in javascript with case conversion

I want to make the first character of every word in a string to uppercase.
i am referring to this article Replacement Text Case Conversion.
when i am running the regular expression ([a-zA-Z])([a-zA-Z](\s)) with the replacement text as \u$1\l$2 in my editor (sublime text) it works fine.
However, when i am trying to do the same in javascript using replace method as below, its giving syntax errors and hence fails.
var regex = /([a-zA-Z])([a-zA-Z]*(\s)*)/gi;
var rep = "\\u$1\\l$2"; // here it is giving error
var result = input.replace(regex,rep);
How to resolve this?
I know this problem can be solved using charAt() and toUppercase() method. but I want to do it using regex with replace. :)
JS regex engine does not support lower- and uppercasing operators \u and \l in the replacement patterns. Use a callback inside String#replace:
var input = "aZ";
var regex = /([a-zA-Z])([a-zA-Z]*(\s)*)/gi;
var result = input.replace(regex, function($0,$1,$2,$3) {
return $1.toUpperCase() + $2.toLowerCase();
});
console.log(result);
Note that you can reduce your pattern to /([a-z])([a-z]*\s*)/gi.

Javascript - Regular expressions Not consistent match

I am getting crazy with this problem, I try to extract "Latin" sentences from the following string (shorter than the original) :
My Name is Yoann
ホームインサイト最新のインサイト運用チームからの最新のマーケ
ットアップデート、運用アップデートマーケットアップデート市場環境情報に関す
るレポート投資アップデート投資環境情報レポート及び出版物運用戦略運用戦略
ファーストステート・スチュワートアジア・パシフィック、グローバル・エマージング・マーケット、ワールドワイド株式
Hello World
Here is the regular expression :
...
//text=My Name is Yoann...
pattern = new RegExp("([A-Za-z]+[\s]?[A-Za-z])+", "g");
results= text.match(pattern);
When I run that, I get :
//results[0]="My"
//results[1]="Name"
//results[2]="is"
//results[3]="Yoann"
//...
The words are splitted on "space". When I try on the http://regexlib.com (JS client engine) tester or http://regexpal.com, the results are :
//results[0]="My Name is Yoann"
//results[1]="Hello World"
So I don't understand what i do wrong in my code but I don't get the same results.
Thanks for your help.
Yoann
> "foo bar".match(new RegExp("[a-z]\s[a-z]"))
null
> "foo bar".match(new RegExp("[a-z]\\s[a-z]"))
["o b"]
When using the RegExp constructor, double your slashes. Better yet, use regex literals, e.g.
/[A-Z][A-Z\s]*[A-Z]|[A-Z]/gi
http://jsfiddle.net/4PdJh/1

Categories

Resources