I am trying to achieve wild card search on a string array using java script
Here the wild cards i use are ? -to represent single char and * to represent multiple char
here is my string array
var sample = new Array();
sample[0] = 'abstract';
sample[1] = 'cabinet';
sample[2] = 'computer';
For example i searched for a string 'ab*t' in the array and the regular expression I used for this is '\ab.*t\', but the problem is I get both 'abstract' and 'cabinet' as matching strings. I only want the string that starts with 'ab' and not where it comes in the middle.
So I modified my regexp like this '\^ab.*t$\ but still the same result. So can somebody give me some tips on how this can be achieved.
You are using using wrong wrong slashs you should use forward slash('/') instead of backward slashs ('\')
probably it'll help you /^ab.*t$/
Related
This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Get Substring between two characters using javascript
(24 answers)
Closed 2 years ago.
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 👍
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);
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.
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()
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);
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".
I have a Javascript array of string that contains urls like:
http://www.example.com.tr/?first=DSPN47ZTE1BGMR&second=NECEFT8RYD
http://www.example.com.tr/?first=RTR22414242144&second=YUUSADASFF
http://www.example.com.tr/?first=KOSDFASEWQESAS&second=VERERQWWFA
http://www.example.com.tr/?first=POLUJYUSD41234&second=13F241DASD
http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD
I want to extract "first" query parameter values from these url.
I mean i need values DSPN47ZTE1BGMR, RTR22414242144, KOSDFASEWQESAS, POLUJYUSD41234, 54SADFD14242RD
Because i am not good using regex, i couldnt find a way to extract these values from the array. Any help will be appreciated
Instead of using regex, why not just create a URL object out of the string and extract the parameters natively?
let url = new URL("http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD");
console.log(url.searchParams.get("first")); // -> "54SADFD14242RD"
If you don't know the name of the first parameter, you can still manually search the query string using the URL constructor.
let url = new URL("http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD");
console.log(url.search.match(/\?([^&$]+)/)[1]); // -> "54SADFD14242RD"
The index of the search represents the parameter's position (with index zero being the whole matched string). Note that .match returns null for no matches, so the code above would throw an error if there's no parameters in the URL.
Does it have to use regex? Would something like the following work:
var x = 'http://www.example.com.tr/?first=DSPN47ZTE1BGMR&second=NECEFT8RYD';
x.split('?first=')[1].split('&second')[0];
Try this regex:
first=([^&]*)
Capture the contents of Group 1
Click for Demo
Code
Explanation:
first= - matches first=
([^&]*) - matches 0+ occurences of any character that is not a & and stores it in Group 1
You can use
(?<=\?first=)[^&]+?
(?<=\?first=) - positive look behind to match ?first=
[^&]+? - Matches any character up to & (lazy mode)
Demo
Without positive look behind you do like this
let str = `http://www.example.com.tr/?first=DSPN47ZTE1BGMR&second=NECEFT8RYD
http://www.example.com.tr/?first=RTR22414242144&second=YUUSADASFF
http://www.example.com.tr/?first=KOSDFASEWQESAS&second=VERERQWWFA
http://www.example.com.tr/?first=POLUJYUSD41234&second=13F241DASD
http://www.example.com.tr/?first=54SADFD14242RD&second=TYY42412DD`
let op = str.match(/\?first=([^&]+)/g).map(e=> e.split('=')[1])
console.log(op)
I am trying to do an interesting task and currently have no idea how to do it.
I have a wiki page (ex: https://en.wikipedia.org/wiki/Moldova ) and I want to save each word from this page into an array. Further I will need to parse this array to extract some specific words.
Can someone give me a hint how can I save words from a text into an array.
And how can I solve this problem:
-For each word remove punctuation such as ,.()"' etc.
-If the words is an html tag , don't store it.
Thank you.
By using the split() method, it is used to split a string into an array of substrings, and returns the new array. Read more about it here.
var text="your text";
var punctRE = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?#\[\]^_`{|}~]/g;
text.replace(punctRE, ''); // Strip all punctuation from the string.
var myArray=text.split(" "); // Pass an empty space as a separator.
Ok, So I hit a little bit of a snag trying to make a regex.
Essentially, I want a string like:
error=some=new item user=max dateFrom=2013-01-15T05:00:00.000Z dateTo=2013-01-16T05:00:00.000Z
to be parsed to read
error=some=new item
user=max
dateFrom=2013-01-15T05:00:00.000Z
ateTo=2013-01-16T05:00:00.000Z
So I want it to pull known keywords, and ignore other strings that have =.
My current regex looks like this:
(error|user|dateFrom|dateTo|timeFrom|timeTo|hang)\=[\w\s\f\-\:]+(?![(error|user|dateFrom|dateTo|timeFrom|timeTo|hang)\=])
So I'm using known keywords to be used dynamically so I can list them as being know.
How could I write it to include this requirement?
You could use a replace like so:
var input = "error=some=new item user=max dateFrom=2013-01-15T05:00:00.000Z dateTo=2013-01-16T05:00:00.000Z";
var result = input.replace(/\s*\b((?:error|user|dateFrom|dateTo|timeFrom|timeTo|hang)=)/g, "\n$1");
result = result.replace(/^\r?\n/, ""); // remove the first line
Result:
error=some=new item
user=max
dateFrom=2013-01-15T05:00:00.000Z
dateTo=2013-01-16T05:00:00.000Z
Another way to tokenize the string:
var tokens = inputString.split(/ (?=[^= ]+=)/);
The regex looks for space that is succeeded by (a non-space-non-equal-sign sequence that ends with a =), and split at those spaces.
Result:
["error=some=new item", "user=max", "dateFrom=2013-01-15T05:00:00.000Z", "dateTo=2013-01-16T05:00:00.000Z"]
Using the technique above and adapt your regex from your question:
var tokens = inputString.split(/(?=\b(?:error|user|dateFrom|dateTo|timeFrom|timeTo|hang)=)/);
This will correctly split the input pointed out by Qtax mentioned in the comment: "error=user=max foo=bar"
["error=", "user=max foo=bar"]
I am trying to parse a webpage and to get the number reference after <li>YM#. For example I need to get 1234-234234 in a variable from the HTML that contains
<li>YM# 1234-234234 </li>
Many thanks for your help someone!
Rich
currently, your regex only matches if there is a single number before the dash and a single number after it. This will let you get one or more numbers in each place instead:
/YM#[0-9]+-[0-9]+/g
Then, you also need to capture it, so we use a cgroup to captue it:
/YM#([0-9]+-[0-9]+)/g
Then we need to refer to the capture group again, so we use the following code instead of the String.match
var regex = /YM#([0-9]+-[0-9]+)/g;
var match = regex.exec(text);
var id = match[1];
// 0: match of entire regex
// after that, each of the groups gets a number
(?!<li>YM#\s)([\d-]+)
http://regexr.com?30ng5
This will match the numbers.
Try this:
(<li>[^#<>]*?# *)([\d\-]+)\b
and get the result in $2.