How do I get the contents of a string?

Source: Internet
Author: User
$str = "Hello, welcome to [ARGS1]. We will ship to you immediately, address: [ARGS2]";

How to get [ARGS1] and [ARGS2]; Is it normal to match?

The regular level is a bit rotten, /^(\[)*(\])$/ is there a problem with matching?
Please advise!

Reply content:

$str = "Hello, welcome to [ARGS1]. We will ship to you immediately, address: [ARGS2]";

How to get [ARGS1] and [ARGS2]; Is it normal to match?

The regular level is a bit rotten, /^(\[)*(\])$/ is there a problem with matching?
Please advise!

JavaScript Edition

With match

var str = '你好,欢迎使用[args1].我们会马上给你发货,地址:[args2]';var re = /\[\w+\]/g;var matches = str.match(re);console.log(matches[0],matches[1]);

With exec

var str = '你好,欢迎使用[args1].我们会马上给你发货,地址:[args2]';var re = /\[\w+\]/g;while((matches =re.exec(str)) != null){    console.log(matches[0]);}

It's complicated.

var str = '你好,欢迎使用[args1].我们会马上给你发货,地址:[args2]';var re = /[\u4e00-\u9fa5]/;console.log(str.split(re).filter(function(value){return /\[\w+\]/.test(value)}))

You also need to make a simple return array.

Article: JavaScript Regular expressions
Regular expression Notes

PHP version

Preg_match


  
   ";     echo $matches[2]."
";}?>

Preg_match_all


  
   ";     echo $matches[0][1]."
";}?>

Related article: strings and regular Expressions in PHP

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.