Solved

Find words in string and add double quotes to it

Posted on 2011-03-16
3
373 Views
Last Modified: 2012-06-27
Hi Experts,

I wonder if this is possible.

I have a string that looks like so: '{series:block1,singletonLoad:null}'

and I need to make it look like so;- {"series":"block1","singletonLoad":"null"}

how do i achieve the desired result using regular expressions please.

Thank you
0
Comment
Question by:claracruz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 250 total points
ID: 35150932
<script>
var a =  '{series:block1,singletonLoad:null}';
alert(a)

a = a.replace(/([^\{\}\,\:]+)/g,"\"$1\"")
alert(a)
</script>

Open in new window

0
 
LVL 14

Expert Comment

by:sentner
ID: 35150984
Depends on the regex version you're using, but this should work with perl's engine:

s/(\w+)/\"$1\"/g
0
 
LVL 6

Expert Comment

by:ramelong
ID: 35153962
for a generic one similar to the previous (@senter), but more compatible:
a = a.replace(/([a-zA-Z0-9]+)/g,'"$1"');
alert(a);

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question