Javascript: How can I search and replace multiple items with matched groups? - javascript

Say I have a string like
"item:(one|two|three), item2:(x|y)"
Is there a single regex that could "factor" it into
"item:one, item:two, item:three, item2:x, item2:y"
Or must I resort to splitting and looping?
If I must split it up then how do I even turn
"item:(one|two|three)"
into
"item:one, item:two, item:three"
if the amount of things between the parentheses is variable? Are regexes useless for such a problem?

You could do it with a callback function:
str = str.replace(/(\w+):\(([^)]*)\)/gi, function(match,item,values)
{return item + ':' + values.split("|").join(', '+item+':')}
);
For every item, the first parentheses in the regex capture the item's name (i.e item) and the second set of (unescaped) parentheses capture the string of all values (i.e one|two|three). The latter are then split at | and joined together with , itemname: and then there is another item name appended to the beginning of the result.
This is probably the easiest way to combine regexes to find your data and split and join to build your new regex. The problem why it is not easier is, that you cannot capture an arbitrary number of consecutive values (one|two|three) in different capturing groups. You would only get the last one, if you tried to capture them individually.

Related

Replace a multiple times a substring in javascript

I need to replace everything between : and , with a | multiple times.
I have a server list like server1:127.0.0.1,server2:127.0.0.2,server3:127.0.0.3.
Basically, I need to remove all the IPs and replace them with some |.
So far I was able to do this:
resultList = serverList.replace(/:.*,/g, '|')
The problem is that the result list is server1|server3:127.0.0.3.
How can I replace every occurrence?
/:.*,/ is greedily matching :127.0.0.1,server2:127.0.0.2. Remember that quantifiers like * will match as much as they can while still allowing the rest of the pattern to match.
Consider specifying [^,] instead of .. This will exclude commas from matching and therefore limit the match to just the region you want to remove.
resultList = serverList.replace(/:[^,]*,/g, '|')
You could take a lazy approach with ? (Matches as few characters as possible).
var string = 'server1:127.0.0.1,server2:127.0.0.2,server3:127.0.0.3';
console.log(string.replace(/:.*?(,|$)/g, '|'));

Javascript regex pattern match multiple strings ( AND, OR, NEAR/n, P/n )

I need to filter a collection of strings based on a rather complex query
I have query input as a string
var query1 ='Abbott near/10 (assay* OR test* ) AND BLOOD near/10 (Point P/1 Care)';
From this query INPUT string I want to collect just the important words:
var words= 'Abbott assay* test* BLOOD Point care';
The query can change for example:
var query2='(assay* OR test* OR analy* OR array) OR (Abbott p/1 Point P/1 Care)';
from this query need to collect
var words='assay* test* analy* array Abbott Point Care';
I'm looking for your suggestion.
Thanks.
You may just use | in your regex to capture the words and/or special characters that you want to remove:
([()]|AND|OR|(NEAR|P)\/\d+) ?
DEMO: https://regex101.com/r/rqpmXr/2
Note the /gi in the regex options, with i meaning that it's case insensitive.
EXPLANATION:
([()]|AND|OR|(NEAR|P)\/\d+) - This is a capture group containing all the words you specified in your title, plus the parentheses.
(NEAR|P)\/\d+ - Just to clear out this part, \d+ means that one or more digits are following the words NEAR or P.
 ? - This captures the possible trailing space after the captured word.

How does split and join work in JavaScript?

But still dont understand : as in input box initially there is no data , however when the user starts typing the functions starts invoking the method in it , however the first line will [ $(this).val().split("-").join(""); ] will then look for the hyphen character to split into ... but as if the data typed by the user dosent contains any hyphens so what has to be replaced...??? like you explained above e.g. split("-") on "a-b-c" will give ["a","b","c"] ....this data already contains the hyphen character and which gets replaced with the character that we specify in brackets. Again at the other hand i dont understand this too : foo = foo.match(new RegExp('.{1,4}', 'g')).join("-"); , why there is single quotes in the RegExp, and what for the 1,4 stands for ..??? as far a i know that it must be meant for minimum 1 and maximum 4 charachters..? could you please help me understanding this..??? Appreciate your help
The function calls are evaluated from left to right. First val(), then split("-")and then join("").
What this does, is that it reads the value and since it seems to be a credit card number, the value will be something like 1234-5678-9012-3456.
The next thing is split the numbers at the hyphens with split("-"). This will result in a kind of list ['1234', '5678', '9012', '3456'].
This list is then joined using "" (Nothing actually) resulting in 1234567890123456, the number without the hyphens.
The same thing could be achieved with
$(this).val().replace(/-/g, "")
using Regular Expressions.
Hope this clarifies stuff!
$(this).val()
Gets the value of the element:
.split("-")
Build an array of strings that splits on '-' (hyphen). Basically, when you find '-' in the string, add it to i.e. 'foo-bar' becomes ['foo', 'bar'].
.join("");
Take the array and join each element back together with an empty string i.e. ['foo', 'bar'] becomes 'foobar'.
Split and join in one line
const result = "7.83".split('.').reduce((first, second) => `${first}' ${second}"`)
console.log(result)
7' 83"

Regex to capture everything but consecutive newlines

What is the best way to capture everything except when faced with two or more new lines?
ex:
name1
address1
zipcode
name2
address2
zipcode
name3
address3
zipcode
One regex I considered was /[^\n\n]*\s*/g. But this stops when it is faced with a single \n character.
Another way I considered was /((?:.*(?=\n\n)))\s*/g. But this seems to only capture the last line ignoring the previous lines.
What is the best way to handle similar situation?
UPDATE
You can consider replacing the variable length separator with some known fixed length string not appearing in your processed text and then split. For instance:
> var s = "Hi\n\n\nBye\nCiao";
> var x = s.replace(/\n{2,}/, "#");
> x.split("#");
["Hi", "Bye
Ciao"]
I think it is an elegant solution. You could also use the following somewhat contrived regex
> s.match(/((?!\n{2,})[\s\S])+/g);
["Hi", "
Bye
Ciao"]
and then process the resulting array by applying the trim() string method to its members in order to get rid of any \n at the beginning/end of every string in the array.
((.+)\n?)*(you probably want to make the groups non-capturing, left it as is for readability)
The inner part (.+)\n? means "non-empty line" (at least one non-newline character as . does not match newlines unless the appropriate flag is set, followed by an optional newline)
Then, that is repeated an arbitrary number of times (matching an entire block of non-blank lines).
However, depending on what you are doing, regexp probably is not the answer you are looking for. Are you sure just splitting the string by \n\n won't do what you want?
Do you have to use regex? The solution is simple without it.
var data = 'name1...';
var matches = data.split('\n\n');
To access an individual sub section split it by \n again.
//the first section's name
var name = matches[0].split('\n')[0];

How to create multiple variation array from a string using javascript

I have this string:
foo = "moon is.white #small"
I want to convert it to this array using rules such as after "." and "#" split and add & if there is no white space before it. I got the answer in this thread: How to Split string with multiple rules in javascript and this is what I got and it is great:
fooArr[0] = ['moon','is','&.white','#small']
My Question is - How do I make other variations of the string to get multiple-variation Array with these rules
A. split in different positions (sometimes the spaces will split and sometimes 2 words and more will be considers as one - in ALL variations (pay attention that if I don't split words with "." or "#" then I don't add "&".
fooArr[1] = ['moon is','&.white','#small']
fooArr[2] = ['moon is.white','#small']
foorArr[3] = ['moon','is.white','#small']
etc...
B. If there is "." or "#" then I want all variations of order between them -- ".all#is.good" can be --> [.all#is.good] & [.all.good#is] & [.good.all#is] etc... (and I want it combined with variations from the first rule such as [.all,&.good,&#is] & [.all.good,$#is]) so I will have ALL COMBINATIONS OF BOTH A AND B
Eventually I need an array combining all the combinations of A and B (it should give me a lot of variations: fooArr[0]..fooArr[X].
Where do I start?
Use a series of regular expression to find all the places where you want to split the string and replace it with " &". Then split on " ".
Or, use a loop to move through the string create replacements like above. And then split.
Or, use a loop to move through the string and build the array directly.

Categories

Resources