2

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.

Community
  • 1
  • 1
DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
  • 1
    Can you include the two words in your regex initially, and then replace it with something else that also includes the two words? – andi Aug 22 '13 at 19:03
  • 1
    You can just replace the SELECT back... can't you not? – Jerry Aug 22 '13 at 19:03
  • possible duplicate of [Regex Match all characters between two strings](http://stackoverflow.com/questions/6109882/regex-match-all-characters-between-two-strings) – DevlshOne Aug 22 '13 at 19:04
  • Why do you need to replace it? Just create a new string. No need to mess around with regular expressions. – James Coyle Aug 22 '13 at 19:06
  • 1
    I suggest you have a look at matching groups. See this post http://stackoverflow.com/questions/432493/how-do-you-access-the-matched-groups-in-a-javascript-regex – Nodebody Aug 22 '13 at 19:07
  • 1
    @DontVoteMeDown:- Tried to implement what you wanted. Check my answer. I have also pasted the sample JSFiddle!!! :) – Rahul Tripathi Aug 22 '13 at 19:19
  • @DevlshOne just for your information, if you **read** the post you will see that I have mentioned that question and said that it not worked for me because is a *regex* only question, not specific for JavaScript, so that answers doesn't works for JavaScript. Thank you. – DontVoteMeDown Aug 22 '13 at 19:53

5 Answers5

7

Just use a capturing group:

"SELECT a, b, c, d FROM".replace(/(SELECT)(.+?)(?= FROM)/, "$1 count(*)")
georg
  • 211,518
  • 52
  • 313
  • 390
  • 2
    But is it really necessary to capture something you're certain what the contents are? I mean, you will _always_ capture `SELECT` and wouldn't it be faster and consume less memory to just put the literal `SELECT` in the replace string? – Jerry Aug 22 '13 at 19:46
  • @Jerry: for this particular case it doesn't make a big difference, but generally I'd prefer captures over literals - just for DRY's sake. – georg Aug 22 '13 at 22:21
2

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/

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Well, you can just put the SELECT back like I said in my comment:

str.replace(/SELECT (.*?)(?= FROM)/i, "SELECT Count(*)");
Jerry
  • 70,495
  • 13
  • 100
  • 144
1

Just as an alternative to regular expressions for this specific string:

str = 'SELECT COUNT(*) ' + str.substr(str.indexOf('FROM'));
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
1

Without regex:

var query = "SELECT a, b, c, d FROM";
var iSelect = query.indexOf("SELECT");
var selLen = "SELECT".length;
var iFrom = query.indexOf("FROM");
if (iSelect >= 0 && iFrom >= 0) {
    query = query.replace(query.substring(iSelect + selLen, iFrom), " COUNT(*) ");
    console.log(query);
}
Fuzzley
  • 306
  • 2
  • 8