mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 s it possible to use a variable as a modifier for a regex pattern. for example: Code: mod = "^"; match = "string to match"; string.match(mod + match); Quote Link to comment https://forums.phpfreaks.com/topic/169126-regex-using-a-variable-as-a-modifier/ Share on other sites More sharing options...
Psycho Posted August 6, 2009 Share Posted August 6, 2009 Yes, but you'll probably need to use the regular expression object. The formatting of the regex is slightly different. But, here's an example: This would find if the letter 'O' appears twice in succession in a string: string.match(/O{2}/) To make the letter you are testing against dynamic, you could use the regular expression object. Notice that when creating the regex object that the beginning and ending forward slashes are dropped. Also, there are specific modification that need to be make for keywords. function matchCustom(string, searchStr) { var regEx = new RegExp(searchStr+'{2}') if (string.match(regEx)) { alert('true'); } else { alert('false'); } } See this page for more info: http://www.regular-expressions.info/javascript.html Quote Link to comment https://forums.phpfreaks.com/topic/169126-regex-using-a-variable-as-a-modifier/#findComment-892388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.