-2

I have a text for example like this:

”Ordernumber 123888 Item 10 SCD”

This I load into an array.

What is the fastest way, to get the string between ”Ordernumber” and ”Item” out of this array? In this case the number ”123888”

I just started with js.

In my real project the text has multiple lines. And out of it I have to get several other information also (sometimes the last strong, mostly between other strings which are always the same...)

In VBA I was able to load all the text into an array and wrote the VBA with find/search etc.

Now I want to transfer my tool to Google Sheets so I just started to learn/use Javascript.

Thanks in advance for your support and feedbacks go my first question in this community

5 Answers5

1

You could use a regex replacement:

var input = "Ordernumber 123888 Item 10 SCD";
var number = input.replace(/\bOrdernumber (\d+) Item\b.*/, "$1");
console.log(number);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Assuming the words are split using spaces, you can split the string and store it into an array.

const str = ”Ordernumber 123888 Item 10 SCD”; const data = str.split(' '); console.log(data[1]);

Now you can get the data by Index.

vanshaj
  • 499
  • 1
  • 4
  • 13
0

I think @Tim Biegeleisen using regex is better, but another approach to achieve this is by getting the start index of "ordernumber" from the string, and then the same for "Item".

And by using String.prototype.substring(), and providing those indexes you can get the text between those words like so:

const str = "Ordernumber 123888 Item 10 SCD"

const res = str.substring(str.indexOf('Ordernumber') + 'Ordernumber'.length, str.indexOf('Item'));

console.log(res);

String.prototype.indexOf()
String.prototype.substring()

zb22
  • 3,126
  • 3
  • 19
  • 34
0

You can use .split () to get the string between one word or another, that way, you will also be able to access the properties of the informed quantity also if you want. But realizing that it is a sales array, I suggest that you do a decapsulation of your object to be able to better manipulate the data and avoid losing them by being all in a single string.

 let string = 'Ordernumber 123888 Item 10 SCD';    
 let result = string.split('Ordernumber').pop().split('Item')[0];
 console.log(result);
0

I don't believe this is a good way of automating things. Searching in strings is prone to errors and is quite difficult to ensure you are extracting what you are really searching for. This is due to word repetitions etc. However, one possible solution for you example above is using regular expressions. For your example you can use this (I put in purpose your string in an array as you said):

var c = [”Ordernumber 123888 Item 10 SCD”]
yourNumberThatYouWant = c[0].match(/Ordernumber (.*) Item/)[1]

This will work for short strings where you have these words only once. If you will have for instance the word "Item" repeating elsewhere the solution will bring everything between first instance of "Ordernumber" and last instance of "Item".

CVname
  • 347
  • 3
  • 12