Click here to Skip to main content
13,150,622 members (39,338 online)
Rate this:
Please Sign up or sign in to vote.
my string is ...
var patt1='/:)/g';

o/p =

I want o/p

How to do?
I am newbie in regex...

please help to solve this :)
Posted 18-Feb-13 1:29am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hi Aarti,

there are actually two issues with your example.

  1. There is no need for quotes around your pattern literal. Regular Expressions are first class citizens in JavaScript. Look here Regular Expression Cheat Sheet[^] or here W3 Schools RegExp Object[^]

  2. Certain characters in Regular Expressions have a special meaning so they'll have to be escaped if you want the character interpreted literally.

Here a fully working example I cooked up for you:
    <script type="text/javascript">
        var patt1=/\:\)/g; //Notice how there are no quotes and the : and ) are escaped by a \

The alert message shows three asterisks!


— Manfred
Aarti Meswania 19-Feb-13 0:45am
thanks :)

I will Like to share some points I learned

write pattern string between slashes and include g or gi at end respectively for case sensitive & case in-sensitive search
e.g. /pattern /gi

when want to search '/' in pattern then write '\/' way
e.g. want to search.. ':/' then write... /:\//gi

when want to search ')' or '(' in string then also put '\'
e.g. want to search ':)' then /:\)/g

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100