Home > Enterprise >  Best practice regarding separating a substring between characters on Javascript
Best practice regarding separating a substring between characters on Javascript

Time:04-20

Note: I tagged the google-app-script because that's where I work and I don't know if there might be some different nomenclatures that are important for everyone to know when answering the question.

Currently, to find the scores (in the example below it is 0-0) that appear between fixed characters 'FT ' and ')', I do it this way:

var text = 'So its (just 1-2) amazing (FT 0-0) its ours final score';
var length = text.length;
var find_str_ft = 'FT ';
var find_str_ft_length = find_str_ft.length;
var position = text.indexOf(find_str_ft);
var text_right = text.substring(position   find_str_ft_length, length);

var find_str_close_bar = ')';
var position_close_bar = text_right.indexOf(find_str_close_bar);
var text_left_close_bar = text_right.substring(0, position_close_bar);
Logger.log(text_left_close_bar);

log:

0-0

I understand that it is an archaic method and and i would like to know if there is more advanced and correct options to work with, even so that it doesn't take so many steps and/or so many lines of code for this.

CodePudding user response:

A regular expression would make this very easy:

let text = 'So its (just 1-2) amazing (FT 0-0) its ours final score';
let match = text.match(/\(FT\s ([^)] )\)/);
let result = match[1];

You want to match the left side:

\(FT\s 

Which means match (FT and 1 or more whitespace characters.

Then you want to match the actual data that you want:

([^)] )

This matches and captures any character that is not ) (the end of our match) and that appears more than once.

Then you match the right side.

\)

Which is quite literally the ) character.

CodePudding user response:

Matching with regex

function lfunko() {
  let s = "So its (just 1-2) amazing (FT 0-0) its ours final score";
  let m = s.match(/(?<=just )([^\)] )|(?<=FT )([^\)] )/g);
  Logger.log('First: %s Second: %s', m[0],m[1]);
}

Execution log
3:26:04 PM  Notice  Execution started
3:26:05 PM  Info    First: 1-2 Second: 0-0
3:26:05 PM  Notice  Execution completed

Utilizing two positive look behinds ored together.

If you wish to see some real expertise at such a match just add a regex tag and you will see some real masters of regular expressions.

Added Regex tag for you. Wait for @Wiktor Stribiżew to answer before making your final selection.

CodePudding user response:

You can use a capture group and match the digits with the hyphen, where the capture group 1 value is denoted by m[1] in the example code:

\(FT\s (\d -\d )\)

Explanation

  • \( Match (
  • FT\s Match FT and 1 whitespace chars
  • (\d -\d ) Capture group 1, match 1 digits - 1 digits
  • \) Match )

const text = 'So its (just 1-2) amazing (FT 0-0) its ours final score';
const m = text.match(/\(FT\s (\d -\d )\)/);
if (m) {
  console.log(m[1]);
}

If there can be more occurrences using the /g flag:

const text = 'So its (just 1-2) amazing (FT 0-0) its ours final score, and not (FT 1-1)';
console.log(Array.from(text.matchAll(/\(FT\s (\d -\d )\)/g), m => m[1]));

CodePudding user response:

And one more for good measure...

const text = 'So its (just 1-2) amazing (FT 7-10) its ours final score';
const regexScore = /FT\s \d -\d /ig;
const regexDigits = /\d /g
const found = text.match(regexScore);
const score = found[0].match(regexDigits);

console.log(found);
console.log(score);

CodePudding user response:

I'm sure it's not the absolute best, and I can imagine places where it's impractical, but a generalized solution I like for many use cases is below:

var str = 'BeginingoflongstringFToranystringScoreorwhateverwewant)oranystringEndoflongstring';
var first_filter = 'FToranystring';
var second_filter = ')oranystring';
var desired_string = str.slice(
  str.indexOf(first_filter)   first_filter.length,
  str.indexOf(second_filter),
);
console.log(desired_string);

And with your specific data we make one change, using lastIndexOf, rather than indexOf since it's a non-unique phrase in your string. To use it generally, it's best to only search for a string between two unique strings:

var str = 'So its (just 1-2) amazing (FT 0-0) its ours final score';
var first_filter = 'FT';
var second_filter = ')';
var desired_string = str.slice(
  str.indexOf(first_filter)   first_filter.length,
  str.lastIndexOf(second_filter),
);
console.log(desired_string);

Or, rather than just using the closing ")" in this case, you can use ") i":

var str = 'So its (just 1-2) amazing (FT 0-0) its ours final score';
var first_filter = 'FT';
var second_filter = ') i';
var desired_string = str.slice(
  str.indexOf(first_filter)   first_filter.length,
  str.indexOf(second_filter),
);
console.log(desired_string);

In any case it requires knowing your input string perfectly, so is rarely ideal.

CodePudding user response:

Alternate Method:

You can also use enter image description here

  • Related