JavaScript equivalent RegExp for PHP - javascript

Im looking for JavaScript equivalent RegExp for PHP.
This PHP RegExp class must have same methods with same returns as JavaScript version.
I can not find anything.
Thanks
[EDIT]
I need same class in PHP as is in JavaScript. preg_match methods in php has different results as RegExp.exec in JavaScript. And also has different flags

You can use Javascript RegExp object in order to accomplish what you want.
Here's an example taken from MDN:
var re = /(\w+)\s(\w+)/;
var str = 'John Smith';
var newstr = str.replace(re, '$2, $1');
console.log(newstr);
You can check more of them here.

I really don't believe you searched all that hard. Top google result for "PHP RegEx": http://php.net/manual/en/function.preg-match.php

Related

str replace all in Javascript

I am trying to some some urls throught javascript where some replacement of urls needs to be done. I have a textarea with some URLs example given below:
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=1
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=2
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=3
http://mywebsite.com/preview.aspx?mode=desktop&url=http://mywebsite.com/post.aspx?id=44&content=3
Now what i am trying to do is replacing http://mywebsite.com/preview.aspx?mode=desktop&url= with spaces.
I have tried using str.replace() but it is replacing only first occurence of that url.
I have also tried with Global variable g the query i have used is
str_replace(\http://mywebsite.com/preview.aspx?mode=desktop&url=/g,'');
But its not working So can anyone tell me how i can do that ?
I want the output of the textarea like:
http://mywebsite.com/post.aspx?id=44&content=1
http://mywebsite.com/post.aspx?id=44&content=2
http://mywebsite.com/post.aspx?id=44&content=3
http://mywebsite.com/post.aspx?id=44&content=4
I believe that your biggest issue is that your regex syntax is incorrect. Try this:
Imagine that var s is equal the the value of your textarea.
s.replace(/http\:\/\/mywebsite\.com\/preview.aspx\?mode\=desktop\&url\=/g, '');
The issue you were having was improper delimiters and unescaped reserved symbols.
Though Javascript has some of its own regex idiosyncrasies, the issues here were related to basic regex, you might find these resources useful:
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
http://regexpal.com
try this.
var string = document.getElementById('textareaidhere');
string.replace(/http:\/\/mywebsite\.com\/preview\.aspxmode=desktop&url=/g, '');
JSFiddle here

JavaScript Regex expression to extract data from html comment

There is a html comment with an Id that I need to extract. The comment is on a div, which is not hard to get using the JQuery $ operator. But the correct RegEx string I need I have not been able to figure out. This is the comment:
<!-- sid=FFKK12H1 -->
And I need a JS variable that has the string "FFKK12H1" assigned to. What is the correct syntax/expression to use? thanks!
EDIT:
I forgot a very important piece of information: The code needs to work on IE7. Unfortunately this is the browser my company allows us to use, and none of the proposed solutions work there so far. Any other thoughs?
The regular expression would be: /<!-- sid=(.+?) -->/i:
var str = '<!-- sid=FFKK12H1 -->';
console.log(str.match(/<!-- sid=(.+?) -->/i)[1]);
var content = $('#comment-containg-div').html();
var regex = /<!--\s*sid=([\x00-\x7F]+)\s*-->/;
var matches = regex.exec(content);
console.log(matches);
The regex here is a amalgamted answer that includes all of the suggestions that other people on the page have made, it seems like it would be the safest to use.
var my_id = my_string.replace(/.*<!-- sid=(.*) -->.*/gi, '$1');
Example
http://jsfiddle.net/YTdKQ/

Javascript regular expressions: substituting variables

I need a hand to sub a variable into a regular expression
this line works fine:
subject = subject.replace(/<a title="Smile"><img.+?<\/a>/g, emoticons[1]);
I need switch the word "Smile" for a variable.
I have tried a few different configurations like:
subject = subject.replace(/<a title="'+emoLanguage[0]+'"><img.+?<\/a>/g, emoticons[1]);
but I can't get the variable to work.
Whats the trick??
First I would say that you probably shouldn't use a regular expression to parse/fix HTML. That being said, this should work:
var re = new RegExp("<a title=\"" + emoLanguage[0] + "\"><img.+?</a>", "g");
subject = subject.replace(re, emoticons[1]);
A better solution would be to use jQuery. The solution is much prettier:
jQuery("a[title='" + emoLanguage[0] + "']").replaceWith(emoticons[1]);
This assumes that the data in emoticons[1] is HTML.
Of course, importing jQuery for just this little thing is probably overkill, but I'm sure that you'll find that it will probably make other things in your Javascript much more easier and elegant.
Use the new Regexp() constructor to create the regular expression, instead of a literal regexp.
Note that you’ll have to properly escape your string, since many characters have special meaning in a regexp.
You have to use the RegExp object cobnstructor in such case:
var pattern = new RegExp('<a title="'+emoLanguage[0]+'"><img.+?</a>',"g");
subject = subject.replace(pattern, emoticons[1]);

Matching regular expression string in Javascript

Does anyone know how to find regular expression string from javascript code?
e.g.
var pattern = /some regular expression/;
Is it possible to to with regular expression :) ?
If I got your question right, and you need a regular expression which would find all the regular expressions in a JavaScript program, then I don't think it is possible. A regular expression in JavaScript does not have to use the // syntax, it can be defined as a string. Even a full-blown JavaScript parser would not be smart enough to detect a regular expression here, for instance:
var re = "abcde";
var regexClass = function() { return RegExp; }
var regex = new regexClass()(re);
So I would give up this idea unless you want to cover only a few very basic cases.
You want a regex to match a regex? Crazy. This might cover the simplest cases.
new RegExp("\/.+\/")
However, I peeked into the Javascript Textmate bundle and is has 2 regex for finding a regex start and end.
begin = '(?<=[=(:]|^|return)\s*(/)(?![/*+{}?])'
end = '(/)[igm]*';
Which you could probably use as inspiration for toward your goal.
Thanks for answers I have found also that it is nearly impossible task to do, but here is my regex which parses source code just fine:
this.mainPattern = new RegExp(//single line comment
"(?://.*$)|"+
//multiline comment
"(/\\*.*?($|\\*/))"+
//single or double quote strings
"|(?:(?:\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")|(?:'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'))"+
//regular expression literal in javascript code
"|(?:(?:[/].+[/])[img]?[\\s]?(?=[;]|[,]|[)]))"+
//brackets
"|([{]|[(]|[\[])|([}]|[)]|[\\]])", 'g');

Invalid regular expression error

I'm trying to retrieve the category part this string "property_id=516&category=featured-properties", so the result should be "featured-properties", and I came up with a regular expression and tested it on this website http://gskinner.com/RegExr/, and it worked as expected, but when I added the regular expression to my javascript code, I had a "Invalid regular expression" error, can anyone tell me what is messing up this code?
Thanks!
var url = "property_id=516&category=featured-properties"
var urlRE = url.match('(?<=(category=))[a-z-]+');
alert(urlRE[0]);
Positive lookbehinds (your ?<=) are not supported in JavaScript environments that do not comply with ECMAScript 2018 standard, which is causing your RegEx to fail.
You can mimic them in a whole bunch of different ways, but this might be a simpler RegEx to get the job done for you:
var url = "property_id=516&category=featured-properties"
var urlRE = url.match(/category=([^&]+)/);
// urlRE => ["category=featured-properties","featured-properties"]
// urlRE[1] => "featured-properties"
That's a super-simple example, but searching StackOverflow for a RegEx pattern to parse URL parameters will turn up more robust examples if you need them.
The syntax is messing up your code.
var urlRE = url.match(/category=([a-z-]+)/);
alert(urlRE[1]);
If you want to parse URL parameters, you can use the getParameterByName() function from this site:
http://james.padolsey.com/javascript/bujs-1-getparameterbyname/
In any case, as already mentioned, regular expressions in JavaScript are not plain strings:
https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
var url = "property_id=516&category=featured-properties",
urlRE = url.match(/(category=)([a-z-]+)/i); //don't forget i if you want to match also uppercase letters in category "property_id=516&category=Featured-Properties"
//urlRE = url.match(/(?<=(category=))[a-z-]+/i); //this is a mess
alert(urlRE[2]);

Categories