Check whether the string contains the specified string by regex

Output

Found target


Explanation

regex expression “tar?get” means “r” can be lacked.


Code examples


JavaScript

let value = "This is target";
if ( value.match(/tar?get/) ) {
    console.log("Found target");
}

Perl

my $value = "This is target";
if ( $value=~ m!tar?get!s ) {
    print "Found target\n";
}

PHP

<?php
$value = "This is target";
if (preg_match('/tar?get/s', $value)) {
    print "Found target\n";
}

Python

import re
value = "This is target"
if re.search( r'tar?get', value ):
   print("Found target")

Ruby

value = "This is target";
if value.match("tar?get")
  print "Found target\n";
end