2

I have an expression.

var expression = "Q101='You will have an answer here like a string for instance.'"

I have a regular expression that searches the expression.

var regEx = new regExp(/=|<>|like/)

I want to split the expression using the regular expression.

var result = expression.split(regExp)

This will return the following:

["Q101", "'You will have an answer here ", " a string for instance'"]

This is not what I want.

I should have:

["Q101", "'You will have an answer here like a string for instance'"]

How do I use the regular expression above to split only on the first match?

2
  • Can you give an example of splitting on like? Commented Nov 14, 2019 at 23:58
  • @Nick If my expression is "Q101like'This answer uses like twice'", then the result would be ["Q101", "This answer uses like twice"]. Commented Nov 15, 2019 at 0:01

3 Answers 3

3

Since you only want to grab the two parts either side of the first delimiter it might be easier to use String.match and discard the whole match:

var expression = "Q101='You will have an answer here like a string for instance.'";

var parts = expression.match(/^(.*?)(?:=|<>|like)(.*)$/);
parts.shift();
console.log(parts);

expression = "Q101like'This answer uses like twice'";
parts = expression.match(/^(.*?)(?:=|<>|like)(.*)$/);
parts.shift();
console.log(parts);

Sign up to request clarification or add additional context in comments.

Comments

1

JavaScript's split method won't quite do what you want, because it will either split on all matches, or stop after N matches. You need an extra step to find the first match, then split once by the first match using a custom function:

function splitMatch(string, match) {
  var splitString = match[0];
  var result = [
    expression.slice(0, match.index),
    expression.slice(match.index + splitString.length)
  ];
  return result;
}

var expression = "Q101='You will have an answer here like a string for instance.'"

var regEx = new RegExp(/=|<>|like/)
var match = regEx.exec(expression)

if (match) {
  var result = splitMatch(expression, match);
  console.log(result);
}

Comments

1

While JavaScript's split method does have an optional limit parameter, it simply discards the parts of the result that make it too long (unlike, e.g. Python's split). To do this in JS, you'll need to split it manually, considering the length of the match —

const exp = "Q101='You will have an answer here like a string for instance.'"

const splitRxp = /=|<>|like/
const splitPos = exp.search(splitRxp)
const splitStr = exp.match(splitRxp)[0]

const result = splitPos != -1 ? (
  [
    exp.substring(0, splitPos),
    exp.substring(splitPos + splitStr.length),
  ]
) : (
  null
);

console.log(result)

1 Comment

Mozilla states that substr "is considered a legacy function and should be avoided when possible," but the logic is clear. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.