Javascript Regex

Hello all,
Im trying to create a function that would search through a string and then replace :something with '$something'

For example:
select * from internal where id = :id and name=:kn_op_id

would result in

select * from internal where id = '$id' and name='$kn_op_id'

Can anyone help?
Stephen ForlanceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


pls try
var str   = 'select * from internal where id = :id and name=:kn_op_id';
var regex = /:\b(.+?)\b/;
str = str.replace(regex, "'\$$1'");

Open in new window

Stephen ForlanceAuthor Commented:
Hi, for the first match its actually coming back as id = '$1' instead of $id
Dan CraciunIT ConsultantCommented:
result = subject.replace(/:(\w+)/g, "'$$$1'");


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ensure Business Longevity with As-A-Service

Using the as-a-service approach for your business model allows you to grow your revenue stream with new practice areas, without forcing you to part ways with existing clients just because they don’t fit the mold of your new service offerings.

Julian HansenCommented:
Why have you posted this to the JavaScript Topic Area - is there a JavaScript component to it - looks more like MySQL / PHP
Dan CraciunIT ConsultantCommented:
@Julian: that's plain SQL.
If the OP wants to modify SQL queries using JS, where is the problem?
Stephen ForlanceAuthor Commented:
Julian - its a javascript question regarding regex manipulation of a string.
Julian HansenCommented:

Just trying to establish what the background is - when you see an SQL query with $ variables (PHP) in a JavaScript context it raises questions - I was just making sure I completely understand the context before attempting a solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.