Javascript regex exec equivalent in PHP [duplicate] - javascript

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);

Related

PHP: preg_match function in 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

JavaScript equivalent RegExp for PHP

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

Escape backslash and double quote in mongo $text

I have a problem with mongo $text $search a phrase. So i want to create a $search phrase from my variable. $search phrase should look like this: "\"search\"". So in javascript we can escape quote and backslash just adding one more slash: '\\\"search\\\"'. But if we print or pass it somewhere we will get different results in different environments. In browser we will get '\"search\"' in node js (5.x) we will get this '\\"search\\". What is the matter?
My final goal is built $search string for using in mongoDB $text operator. Maybe somebody can help me with it or with my question above.
(I know it's late, but I want to answer in case someone will find this page looking for a way to solve this problem)
You don't have to write it with the right Javascript format. Just leave it as it is in MongoShell
db.collection.find({$text:{$search:'\"search\"'}}, function...
So, a good way to approach a generic string is
var search = "generic phrase to search";
search = '\"' + search.split(' ').join('\" \"') + '\"';
db.collection.find({$text:{$search:search}}, function...
Hope it's helpful.

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);

Create a Javascript RegExp to find opening tags in HTML/php template

I'm trying to write a Javascript HTML/php parser which would extract all opening tags from a HTML/php source and return the type of tag and attributes with their values while at the same time monitoring whether the values/attributes should be evaluated from static text or php variables. The problem is when I try to compose the Javascript RegExp pattern and more specifically certain rare cases. The RegExp I was able to come up with either involve negative lookbehind (to cope with the closing php tag - that is to match a closing bracket that is not preceded by a question mark) or fails in certain cases. The lookbehind version looks like:
<[a-zA-Z]+.*?(?<!\?)>
...and works perfect except for my case which must avoid using lookbehind. A more Javascript friendly version would be:
<[a-zA-Z]+((.(?!</)(?!<[a-zA-Z]+))*)?>
...which works except in this case:
<option value="<?php echo $img; ?>"<?php echo ($hpb[$i]['image_filename']==$img?' selected="selected"':''); ?>><?php echo $img; ?></option>
Am I approaching the problem completely messed up or is the lookbehind really necessary in my case? Any help is greatly appreciated.
Just make sure the last letter before the '>' is not a ?, using [^?]. No lookaheads or -behinds needed.
<[a-zA-Z](.*?[^?])?>
the parentheses and the last ? is to also match tags like <b>.
EDIT The solution didn't work for single character tags without attributes. So here is one that does:
<[a-zA-Z]+(>|.*?[^?]>)
much simpler answer would be <[^/^>]+>

Categories