Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Find words in string and add double quotes to it

Posted on 2011-03-16
3
Medium Priority
?
377 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
3 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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

972 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