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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Rgonzo1971Commented:
Hi,

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

Open in new window

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


HTH,
Dan
0

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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

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.
0
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
JavaScript

From novice to tech pro — start learning today.