This question seems to be simple and repetitive here in SO.
But consider this string: SELECT a, b, c, d FROM. I want to get only what is between SELECT and FROM.
Nice so I have found this answer that propose this regex: (?<=SELECT)(.*)(?=FROM). It's perfect if lookbehind works in JavaScript, according to this post:
Unlike lookaheads, JavaScript doesn't support regex lookbehind syntax
So it won't work(test it in regexpal that is made for JS). This anwser proposes this regex: SELECT=(.*?)FROM. But it includes the two words, so it not fits my needs.
The purpose of this is to use in a replace function to transform this...
SELECT a, b, c, d FROM
into this...
SELECT Count(*) FROM
Thank you in advance.
Just use a capturing group:
"SELECT a, b, c, d FROM".replace(/(SELECT)(.+?)(?= FROM)/, "$1 count(*)")
Try this:-
$("button").click(function() {
var srctext = $("#fixme").text();
console.log("old text: " + srctext);
var newtext = srctext.replace(/(SELECT)(.+?)(?= FROM)/, "$1 count(*)");
console.log("new text: " + newtext);
$("#fixme").text(newtext)
});
WORKING JSFIDDLE:- http://jsfiddle.net/tkP74/1597/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With