How to learn which sites refer to my blog?
regular expression from variable
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What is interesting here?
• Duels
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Register!

QAIX > Javascript web-programming > regular expression from variable 22 August 2005 16:13:18

  Top users: 
  Recent blog posts: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Модератор:

regular expression from variable

Martin John Brindle 22 August 2005 15:01:15
 I need to build a regular expression where the expression contents are
variable.

for example I have a string that i need to search for, but the string can
change. If i have a variable called string I need to look at the contents of
string otherwise

/string/

obviously doesn't work!

Suggestions appreciated.

Martin


Add comment
RobG 22 August 2005 16:13:18 permanent link ]
 Martin John Brindle wrote:> I need to build a regular expression where the expression contents are > variable.>
for example I have a string that i need to search for, but the string can > change. If i have a variable called string I need to look at the contents of > string otherwise>
/string/>
obviously doesn't work!>
Suggestions appreciated.

function searchFor( s, str ) {
var re = new RegExp( s );
return re.test( str );
}

will return true for: searchFor( 'be', 'beach')

To match a word exactly, use:

var re = new RegExp( '\\b' + s + '\\b' );

Now:

searchFor( 'be', 'let it be') // returns true
searchFor( 'be', 'beat it') // returns false

To make the test case insensitive:

var re = new RegExp( '\\b' + s + '\\b', 'i' );

etc.

--
Rob
Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или законы РФ. Ваш ip-адрес записывается.


QAIX > Javascript web-programming > regular expression from variable 22 August 2005 16:13:18

see also:
php 4.3.11 and mysql 5.0.3?
XSLT Conformance Tests
ODBC - What Perl can and PHP not
pass tests:
see also:
Where is everybody?
How to convert MTS/TS/MOD video to…
† Number Zero †

  Copyright © 2001—2010 QAIX
Идея: Монашёв Михаил.
Авторами текстов, изображений и видео, размещённых на этой странице, являются пользователи сайта.
See Help and FAQ in the community support.qaix.com.
Write in the community about the bugs you have noticedbugs.qaix.com.
Write your offers and comments in the communities suggest.qaix.com.
Information for parents.
Пишите нам на .
If you would like to report an abuse of our service, such as a spam message, please .
Если Вы хотите пожаловаться на содержимое этой страницы, пожалуйста .