te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Part of String Case Insensitive in JavaScript Regex (?i) option not working - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Part of String Case Insensitive in JavaScript Regex (?i) option not working - Stack Overflow

programmeradmin4浏览0评论

I am using Nodejs to build an application in which I need to process certain strings I have used the JS "RegExp" object for this purpose. I want only a part of my string in the regex to be case insensitive

var key = '(?i)c(?-i)ustomParam';
var find = '\{(\\b' + key +'\\b:?.*?)\}';
var regex = new RegExp(find,"g");

But it breaks with following error

SyntaxError: Invalid regular expression: /{(\b(?i)c(?-i)ustomParam\b:?.*?)}/

I will get the key from some external source like redis and the string to be matched from some other external source , I want that the first alphabet should be case-Insensitive and the rest of alphabets to be case-Sensitive.

When I get the key from external source I will append the (?i) before the first alphabet and (?-i) after the first alphabet.

I even tried this just for starters sake, but that also didn't work

var key ='customParam';
var find = '(?i)\{(\\b' + key +'\\b:?.*?)\}(?-i)';
var regex = new RegExp(find,"g");

I know I can use "i" flags instead of above ,but that's not my use case. I did it just to check.

I am using Nodejs to build an application in which I need to process certain strings I have used the JS "RegExp" object for this purpose. I want only a part of my string in the regex to be case insensitive

var key = '(?i)c(?-i)ustomParam';
var find = '\{(\\b' + key +'\\b:?.*?)\}';
var regex = new RegExp(find,"g");

But it breaks with following error

SyntaxError: Invalid regular expression: /{(\b(?i)c(?-i)ustomParam\b:?.*?)}/

I will get the key from some external source like redis and the string to be matched from some other external source , I want that the first alphabet should be case-Insensitive and the rest of alphabets to be case-Sensitive.

When I get the key from external source I will append the (?i) before the first alphabet and (?-i) after the first alphabet.

I even tried this just for starters sake, but that also didn't work

var key ='customParam';
var find = '(?i)\{(\\b' + key +'\\b:?.*?)\}(?-i)';
var regex = new RegExp(find,"g");

I know I can use "i" flags instead of above ,but that's not my use case. I did it just to check.

Share Improve this question edited Jan 5, 2024 at 8:16 Mahozad 24.6k19 gold badges159 silver badges179 bronze badges asked May 13, 2016 at 10:43 VipreshVipresh 1,30717 silver badges34 bronze badges 3
  • 1 You cannot make a part of regex case-insensitve, use i flag on plete regex. – Tushar Commented May 13, 2016 at 10:45
  • 3 Bad news: even XRegExp cannot offer this functionality, you can only use (?i) on the whole pattern declaring it at the pattern beginning. In XRegExp, you can define the regex as var regex = XRegExp('(?i)\\{(\\b' + key +'\\b:?.*?)\\}', 'g'); – Wiktor Stribiżew Commented May 13, 2016 at 10:50
  • 1 OKi so JavaScript regex library sucks. @WiktorStribiżew if you had posted your ment as answer I would have accepted it as answer – Vipresh Commented May 13, 2016 at 11:03
Add a ment  | 

2 Answers 2

Reset to default 12

JavaScript built-in RegExp does not support inline modifiers, like (?im), let alone inline modifier groups that can be placed anywhere inside the pattern (like (?i:....)).

Even XRegExp cannot offer this functionality, you can only use (?i) on the whole pattern declaring it at the pattern beginning.

In XRegExp, you can define the regex ONLY as

var regex = XRegExp('(?i)\\{(\\b' + key +'\\b:?.*?)\\}', 'g');

On May 27, 2020, still neither JavaScript native RegExp, nor XRegExp patterns support inline modifier groups (i.e. (?i:...)), nor placing them in any part of the pattern (as far as XRegExp is concerned).

This will be possible starting from Chrome 122 using RegExp modifiers.

To enable a flag for a subexpression, use (?X:subexpr) where X is one of i, m, or s.
To disable a flag for a subexpression, use (?-X:subexpr).

Enable a flag for part of regex:

const r1 = /^[a-z](?i:[a-z])$/;
r1.test("ab"); // true
r1.test("Ab"); // false
r1.test("aB"); // true

Disable a flag for part of regex:

const r2 = /^[a-z](?-i:[a-z])$/i;
r2.test("ab"); // true
r2.test("Ab"); // true
r2.test("aB"); // false
发布评论

评论列表(0)

  1. 暂无评论