【问题标题】:Regular Expression to get a string between parentheses and inside the string literal in Javascript正则表达式在 Javascript 中获取括号之间和字符串文字内部的字符串
【发布时间】:2021-01-11 07:56:10
【问题描述】:
let s =  "Cannot add or update a child row: a foreign key constraint fails (`testdb`.`products`, CONSTRAINT `products_ibfk_3` FOREIGN KEY (`category_id`) REFERENCES `product_categories` (`id`))"

这里我需要用括号 ex 提取文字中的 FOREIGN 键。 extracted = "category_id"。我不知道该怎么办 regexp.

有人,你能帮帮我吗?

提前感谢您对社区的大力支持。

【问题讨论】:

  • 查看捕获组
  • 你想要反引号之间的所有匹配项,还是只需要外键名称?
  • 只是外键

标签: javascript regex string


【解决方案1】:

let s =  "Cannot add or update a child row: a foreign key constraint fails (`testdb`.`products`, CONSTRAINT `products_ibfk_3` FOREIGN KEY (`category_id`) REFERENCES `product_categories` (`id`))";

let match = /FOREIGN KEY \(`([\w]*)`\)/.exec(s);
let extracted = match[1];
console.log(extracted);

编辑:

模式(/FOREIGN KEY (([\w]*))/) 确实从字符串中提取“FOREIGN KEY (category_id)”和“category_id”。请参考this url

【讨论】:

    猜你喜欢
    • 2013-07-20
    • 2011-07-17
    • 2010-09-29
    相关资源
    最近更新 更多