JavaScript regex and replace like PHP preg_replace [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have following code in php:
$link ='mylink';
$description = '<p><img src="http://l3.yimg.com/bt/api/res/1.2/hmYETKsJS2CXBsG7oO125w--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/Reuters/2013-09-07T045234Z_3_CBRE9851BPL00_RTROPTP_2_RUSSIA-G20.JPG">By Roberta Rampton and Susan Cornwell WASHINGTON (Reuters) - ...</p>';
$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$new_des = preg_replace($pattern,"?url=".$link,$description);
How can I do that in javascript?

pattern = /(href=["'])[^"']+(?=["'])/
new_des = description.replace(pattern, "$1?url=" + link)
JS doesn't support lookbehinds, therefore you'll have to capture the first group and insert it back when replacing using $1.

Related

Select text on a string after X symbol [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
i have this string
$string = "Social\Notify\Models\User";
How can i tell to php how select just the fourth segment of it? in this case just the word User?
Something like this will do?
$str='Social\Notify\Models\User';
echo explode('\\',$str)[3]; //"prints" User

how to match rtmp links [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to match the following link structure that started with rtmp://
$link = "rtmp://00.000.000.000/live"; // GOOD
$link = "http://00.000.000.000/live"; // BAD
$link = "www.00.000.000.000/live"; // BAD
so only matching the links with rtmp with preg_match or whatever ~ thanks
I'd do:
preg_match('~^rtmp://~', $link);
Something like this will suffice..
if(stripos($link,'rtmp://')!==false)
{
echo "Good";
}

php query where a word like and skip before words [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
php script query out put is
"some word STAR some other words", I want remove all workd before "Star". I need all other words after "Star".
I using php script output from a mysql file
$newStr = substr_replace($str, '', 0, strpos($str,"STAR"));
Where $str is a variable with a string, eg: "some word STAR some other words"
$arrayStr = explode('STAR', 'some word STAR some other words');
echo $arrayStr[1];
DEMO
Some documentation:
php.net explode() function

Regex to select everything in between two characters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to create a regex that will select the data between two characters (& - #).
& foo #
I would like to select foo.
Any ideas? Good explanations are also appreciated.
how about this:
preg_match('~&([^#]*)#~',$content,$match);

How to parse HTML and preformat using php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have text like this Too early to post? http://www.somewebsite.com/article/226973 I want to parse the hyperlinks in the text and make the text look like so Too early to post? http://www.entrepreneur.com/article/226973 I want to do this but I have no idea where to start from or what regexp to use.
$s = 'Too early to post? https://www.somewebsite.com/article/226973';
$parsed = preg_replace('#(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)#', '$1', $s);
echo $parsed;

Categories