PHP: preg_match function in JavaScript - javascript

I need to test if a string matches following ruby regular expression:
/^[0-9]*[\u007C-]{1}[0-9]*$/
My problem here is, that I am not using PHP, I am using JavaScript... I saw some examples as i searched through google, but i don't get it.
Hopefully someone can help me!

Try this:
function isMatch(str) {
return /^[0-9]*[\u007C-]{1}[0-9]*$/.test(str)
}
console.log(isMatch('|'))

A couple of options:
'some string'.match(/^[0-9]*[\u007C-]{1}[0-9]*$/)
/^[0-9]*[\u007C-]{1}[0-9]*$/.test('a string')
I'm not too familiar with the syntax of Ruby regular expressions, so you might need to adapt the syntax a bit:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Related

Javascript regex exec equivalent in PHP [duplicate]

I have a strings of the form
"Look at this [website]{http://www.stackoverflow.com}
or at this [page]{http://www.google.com}"
and I want to parse them with PHP to
"Look at this <a href='http://wwwstackoverflow.com'>website</a>
or at this <a href='http://www.google.com'>page</a>"
How can I do this?
I though about using str_replace()but I dont know how to get the string between the brackets [].
Edit[26.08.2016]: The answer uses the PHP preg_replace method with Regular Expression. I didnt understand the given answer here, but it worked so I was happy But now I found this free tutorial that teaches you how to use Regular Expression. I found it very helpful. Especially when one wants to wrtie his own Regular Expression for a similar case.
try it with preg_replace
$str="Look at this [website]{http://www.stackoverflow.com} or at this [page]{http://www.google.com}";
echo preg_replace('/\[(.*?)\]\{(.*?)\}/', "<a href='$2'>$1</a>", $str);
output:
Look at this <a href='http://www.stackoverflow.com'>website</a> or at this <a href='http://www.google.com'>page</a>
working Example: https://3v4l.org/jLbff
This can be made easily by using preg_replace:
$pattern = "/(\\[([^\\]]+)\\]\\{([^\\}]+)\\})/";
$replacement = '$2';
$finalStr = preg_replace($pattern, $replacement, $yourString);

Translate Ruby regex into JavaScript

I've got such a regex in ruby on rails
/\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
I'd like to use same email validation logic in front end.
I've tried to use the .inspect method in the irb console, it doesn't seem to return a js valid regular expression.
As far as I understand \A is a ^, \Z is a $. [-a-z0-9] probably translates to [a-zA-Z0-9]. Not sure about the rest.
I've tried to look for an online converter too, couldn't find one. Answers in other similar topics in SO didn't work.
What's the easiest way to translate such regex from ruby into javascript?
As stated by Daniel in the comment it should just translate one to one, I incorrectly assumed that [-a-z0-9] should be replaced.
Ruby version:
/\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
JavaScript version:
/^([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})$/
Some tests:
/^([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})$/i.test("test#email.com"); // true
/^([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})$/i.test("test#emailcom"); // false
/^([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})$/i.test("testemail.com"); // false
Did you try this?
class Regexp
def to_javascript
Regexp.new(inspect.sub('\\A','^').sub('\\Z','$').sub('\\z','$').sub(/^\//,'').sub(/\/[a-z]*$/,'').gsub(/\(\?#.+\)/, '').gsub(/\(\?-\w+:/,'('), self.options).inspect
end
end
When you render it to the client simply instantiate a new RegExp object with the resulting string:
new RegExp(regexpStringFromRuby);
Check the client_side_validations gem. It might be what you need.

regular expression javascript or jquery

I have the following types of query string. It could be anyone of the below.
index.html?cr=33&m=prod&p=ded
index.html?cr=33&m=prod&p=ded&c=ddl&h=33&mj=ori
From the above query string, i wanted to extract only m & p "m=prod&p=ded".
Otherway to do is split and get, that i have already done it.
But I wanted to achieve it using regular expression.
Any help is highly appreciated.
You can use something like this :
(?<=&)m=[^& ]*\&p=[^& ]*
In javascript , It doesn't support ?<= positive Lookbehind.
So you can use the following code ( in javascript ):
var s="index.html?cr=33&m=prod&p=ded"
s.match(/m=[^& ]*\&p=[^& ]*/g)
Refer Regexp101
If both query parameters come one after another then you you can use:
url.match(/[?&]m=([^&]*)&p=([^&]*)/i);

Javascript RegExpression

I am looking for a regular expression that will accept date formats, both MM/DD/YYYY and M/D/YYYY
I found one here
However when I run that I get an unexpected illegal token. Here is how I am implementing the javascript function
return RegExp(/^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/).test(txtDate);
I've been told the http://regexlib.com website usually works well for .net regular expressions, but not javascript.
Any help would be great!
Escape the slashes inside the regex.
/^(((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])\/(29|30)|(0?[13578]|1[02])\/31)\/(19|[2-9]\d)\d{2}|0?2\/29\/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/
here __^ __^ __^ ___^ ___^___^
for MM/DD/YY /^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/
for M/D/Y /^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$/
Recipe 4.4 from Regex Cookbook does most of what you are looking for:
^(1[0-2]|0?[1-9])/(3[01]|[12][0-9]|0?[1-9])/(?:[0-9]{2})?[0-9]{2}$
Take a look at this:
http://jsfiddle.net/oscarj24/32JQr/
hope this helps

Javascript String Parsing

I am trying to parse a string in this format
[something](something something) [something](something something)
and I want to break on every space that is not between a set of parenthesis?
I tried using js string.split with this as the regex /[^\(].*\s+.*[^\)]/g, but it doesn't work? Any suggestions appreciated :-)
EDIT: I don't want to post this as an answer, because I want to leave it open to comments but I finally found a solution.
var a = "the>[the](the the) the>[the](the the) the"
var regex = /\s+(?!\w+[\)])/
var b = a.split(regex)
alert(b.join("+++"))
Is your input always this consistent? If it is, it could be as simple as splitting your string on ') ['
If it isn't, is it possible to just take what is between [ and )? Or is there some kind of nesting that is going on?
You are using the wrong tool for the job.
As was alluded to in this famous post, regular expressions cannot parse non-regular languages, and the "balanced parenthesis" problem cannot be described by a regular language.
Have you tried writing a parser instead?
EDIT:
It seems that you've finally clarified that nesting is not a requirement. In that case, I'd suggest gnur's solution.
This regex will do exactly what you asked, and nothing more:
'[x](x x) [x](x x)'.split(/ +(?![^\(]*\))/);

Categories