Solved

Find words in string and add double quotes to it

Posted on 2011-03-16
3
366 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 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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)

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now