Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Honestly, I am horrible with regular expressions and I needed a ton of help making this PHP regex. Although, I have decided that I need to convert a ton of my code to Javascript.

Here is my PHP regex:

if(preg_match('/^(([^,]+,){4}[^,]+\n){3}$/', $all_questions))

Could someone give me a hand on converting it to a Javascript regex?

Greatly appreciated, thanks!

share|improve this question
    
Regex is common in all language. Not specific. –  Praveen Aug 24 '13 at 11:44
6  
@user1671639 No, it’s not. Furthermore, there are quite some significant differences between the various implementations. –  Gumbo Aug 24 '13 at 11:46
    
@user1671639 Not really. It depends on Regex parser language/technology uses. There're different implementations and they differ slightly but unless you use some advanced features, it isn't noticeable. –  Leri Aug 24 '13 at 11:47
    
Although there are differences between regular expression flavors, the one you are using can be used as is in Javascript without the need for any modification. –  Ibrahim R. Najjar Aug 24 '13 at 11:49
    
@Gumbo, Leri Oh Thank you so much for the pointers. I thought that regex expression tend to be same in all languages. –  Praveen Aug 24 '13 at 11:53

1 Answer 1

up vote 2 down vote accepted

In JS, you can do it simple do a .test() with Regex(no need to change the expression).

var yourRegEx=/^(([^,]+,){4}[^,]+\n){3}$/;
if(yourRegEx.test($all_questions) {  // replace $all_questions with your
                                     // js variable
}
share|improve this answer
    
That is an error since $all_questions is a PHP variable –  Fizzix Aug 24 '13 at 11:53
    
@fizzix you want to test it with a PHP variable? or you have a equivalent js variable for that? –  Praveen Aug 24 '13 at 11:55
    
It is still returning an error what I cant the variable to fullString which is my JS variable name. –  Fizzix Aug 24 '13 at 12:04
    
@fizzix yourRegEx.test('<?= $all_questions; ?>') –  Leri Aug 24 '13 at 12:07
    
@Leri - I won't be using that PHP variable anymore. So it would be if(yourRegEx.test(fullString)){alert("Working")} –  Fizzix Aug 24 '13 at 12:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.