Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between PHP regex and JavaScript regex

Hi i want to use below php regex in spry java script framework but them doesn't work with spry framework and spry doesn't let the user to input!.
1)"/^[\d]+$/"
2)"/^([\x{600}-\x{6FF}]+\s)*[\x{600}-\x{6FF}]+$/u"
3)"/^([\x{600}-\x{6FF}]+\d*\s)*[\x{600}-\x{6FF}]+\d*$/u"
please help me to convert them to use in spry framework.

like image 381
fire boy Avatar asked Jan 30 '12 23:01

fire boy


People also ask

What is PHP regex?

In PHP, regular expressions are strings composed of delimiters, a pattern and optional modifiers. $exp = "/w3schools/i"; In the example above, / is the delimiter, w3schools is the pattern that is being searched for, and i is a modifier that makes the search case-insensitive.

What is JavaScript regex?

RegExp ObjectA regular expression is a pattern of characters. The pattern is used to do pattern-matching "search-and-replace" functions on text. In JavaScript, a RegExp Object is a pattern with Properties and Methods.

Does PHP support regex?

PHP has a built-in support for regular expressions too. In PHP, there are two modules for regular expressions: the POSIX Regex and the PCRE. The POSIX Regex is depreciated. In this chapter, we will use the PCRE examples.

Is JavaScript and Python regex the same?

They are different; One difference is Python supports Unicode and Javascript doesn't. Read Mastering Regular Expressions. It gives information on how to identify the back-end engines (DFA vs NFA vs Hybrid) that a regex flavour uses. It gives tons of information on the different regex flavours out there.


2 Answers

1) /^[\d]+$/
2) /^([\u0600-\u06FF]+\s)*[\u0600-\u06FF]+$/
3) /^([\u0600-\u06FF]+\d*\s)*[\u0600-\u06FF]+\d*$/

/u is not supported, since Javascript regexes only supports unicode in terms of codepoints. \x{???} (unicode codepoints) should be written \u???? in Javascript regex (always 4 digits 0 padded)

In these cases, the following applies to the rest of the regex:

  • \s in Javascript is treated as unicode
  • \d isn't, which means only ASCII digits (0-9) are accepted.

This means we specifically have to allow "foreign" numerals, e.g. Persian (codepoints 06F0-06F9):

1) /^[\d\u06F0-\u06F9]+$/
2) /^([\u0600-\u06FF]+\s)*[\u0600-\u06FF]+$/
3) /^([\u0600-\u06FF]+[\d\u06F0-\u06F9]*\s)*[\u0600-\u06FF]+[\d\u06F0-\u06F9]*$/

(Remove \d if ASCII digits shouldn't be accepted)

Not sure what the brackets are supposed to be doing in example 1, originally they could be written:

1) /^\d+$/

But to add the Persian numerals, we need them, see above.

Update

Spry character masking, however, only wants a regex to be applied on each entered character - i.e., we can't actually do pattern matching, it's just a "list" of accepted characters in all places, in which case:

1      ) /[\u06F0-\u06F9\d]/      // match 0-9 and Persian numerals
2 and 3) /[\u0600-\u06FF\d\s]/    // match all Persian characters (includes numerals), 0-9 and space

Once again, remove \d if you don't want to accept 0-9.

Update 2

Now... using regex for validation with Spry:

var checkFullName = function(value, options)
{
   // Match with the by now well-known regex:
   if (value.match(/^([\u0600-\u06FF]+\s)*[\u0600-\u06FF]+$/))
   {
      return true;
   }
   return false;
}

var sprytextfield =
     new Spry.Widget.ValidationTextField(
          "sprytextfield", 
          "custom", 
          { validation: checkFullName, validateOn: ["blur", "change"] }
     );

A similar custom function can be made for case 3.

See examples from Adobe labs

like image 129
JimmiTh Avatar answered Sep 30 '22 15:09

JimmiTh


Are you passing them in as strings or as regex objects? Try removing the " characters from around the regex.

The 'u' flag is a little more tricky. You may need to explain what the 2nd and 3rd regexes are trying to do.

like image 34
david Avatar answered Sep 30 '22 16:09

david