Split operating without concating [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a string 'OR-xxxxxxxx-001-01'.
I need to split it as 'OR-xxxxxxx-001' and '01'. Is it possible without split by '-' and concatenating again ??

In Java, use lastIndexOf:
String string = "OR-xxxxxxxx-001-01";
int lastDash = string.lastIndexOf('-');
String prefix = string.substring(0, lastDash); // OR-xxxxxxx-001
String suffix = string.substring(lastDash + 1); // 01

This solution is for JavaScript (Java and JavaScript is not the same thing).
Since you don't want to split by - it's possible to do it with indexes. You haven't specified if it's of fixed length or not (which this solution depends on), but this should do the trick:
t = 'OR-xxxxxxxx-001-01'
str = t.slice(0, 12) + t.slice(16);
Will give str = 'OR-xxxxxxxx-01'

You could use a split with a positive look-ahead:
String str = "OR-xxxxxxxx-001-01";
String[] split = str.split("-(?=[^-]+$)");
[^-]: any character other than -.
+: repeated one or more times
$: at the end of the String
Then the (?= ... ) is used for the positive look-ahead, so it won't be removed by the split. This means only the - outside the parenthesis act as delimiter to split on.
Try it online.
PS: In Java.

Related

Regex, how to detect value in double curly braces? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Sorry i can't speak english :(
I write Supplant I have a regex
({{([']).*\2}})
needs regex to detect what is in a rectangle
I need it rigidly between {{' - '}} because there can be any character between the quotation marks (e.g. one { )
best to use it \1 because regex is more longer
Thanks for the help. Regards
This can work for you:
/\{\{'((?:(?!\{\{').)*?)'\}\}/g
\{\{' - start by matching {{'.
( ... ) - if you're writing a template engine you probably care about what's inside the curly braces and quotes. This captures the string inside the quotes so you'll be able to use it.
(?: ... ) - a non-captureing group (the value here will not be used).
(?!\{\{'). - match anything, except if we're seeing another {{'.
(?!\{\{').)*? - *? is a lazy match, so we'll stop at the first '}}
and finally, the closing '}}
as code it will looks something like this - I included a function to set the replaced value, because typically that's what you'd do in a template engine:
let s = "n {{'a{123456789}'}} n {{\"a{123456789}\"}} n {{'a{1234{{'a{12345{{'a{123456789}'}}6789}'}}56789}'}} ";
s = s.replace(/\{\{'((?:(?!\{\{').)*?)'\}\}/g, (wholeMatch, capturedKey) => {
console.log('captured key:', capturedKey);
return "REPLACED " + capturedKey;
});
console.log(s);
if you want to support double quotes it becomes a bit more complicated:
s = s.replace(/\{\{(["'])((?:(?!\{\{["']).)*?)\1\}\}/g,
(wholeMatch, quote, capturedKey) => { ... }
);
Try this:/(?<={{')(?!.+?{{)[^']+/gm
This basically looks for the {{' which doesn't have any other {{ in front of it then, matches everything till the first '.
Test here: https://regex101.com/r/rw6kkP/2
var myString = `{{'a{content 1}a'}}
{{'a{every{{'a{everysign}a'}}sign}a'}}
{{'a{every{{'a{every{{'a{inside content}a'}}sign}a'}}sign}a'}} `;
var myRegexp = /(?<={{')(?!.+?{{)[^']+/gm;
console.log(myString.match(myRegexp))
// [ "a{content 1}a", "a{everysign}a", "a{inside content}a" ]

Javascript Array - Split string at numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an unknown string containing a set of numbers like so:
var string = "stuff 1.23! (456) 789 stuff";
I would like to split the array, in order to modify the numbers and later rejoin the array. The result I'm looking for should look like this:
var result = ['stuff ', 1.23, '! (', 456, ') ', 789, ' stuff'];
Is there a better solution than to loop through each character individually? Thanks!
use a character class to split the values:
/(-?[\d.]+)/
-? May start with a negative such as -123
[\d.]+ Has one or more numbers and decimals
var string = "stuff 1.23! (456) 789 stuff -234".split(/(-?[\d.]+)/);
console.log(string)
The ideal solution really depends on what exactly you are doing with the data. One simple solution is a regular expression with replace.
var string = "stuff 1.23! (456) 789 stuff";
var updated = string.replace(/\d+(\.\d+)?/g, function (m) {
console.log(m);
return "xxx";
})
console.log(updated)
A regular expression is an expression you can use to search within your string, in you case, for digits. Create a regular expression which searches for sequences of digits, and you'll be able to use the split method on your string to create an array of strings like you specified.

Javascript - Regex Does not contain some character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I try to regex for not contain some character.
I need to show /%7(.*?);/g which dose not contain "=".
I try to input
?!xx=1
and change to
?!( (.?)=(.?) )
But it dose not work.
Please help. Thanks.
//Here is my simple regex
reg = /%7((?!xx=1).*?);/g ;
//Here is my string
str = "%7aa; %7bb=11; %7cc=123; %7xx=1; %7yy; %7zz=2;"
//I need
%7aa; and %7yy;
Instead of using a negative lookahead, try using a ^ block:
const reg = /%7([^=;]+);/g;
The ([^=;]+) bit matches any non-=, the condition you're looking for, and non-;, the character at the end of your regex.
I left the capture group in since your question's regex also contains it.
const reg = /%7([^=;]+);/g;
const str = "%7aa; %7bb=11; %7cc=123; %7xx=1; %7yy; %7zz=2;"
const matches = str.match(reg);
console.log(matches);

split a string with comma and colon [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a string like as shown below:
var String = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:some string without comma,xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:some string,with comma,xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:some string,xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:some string without comma,xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:some string , with comma,xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:String,with comma"
Where xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx represents an alphanumeric generated Id and after the colon is a string related to that Id.The string can be a string with comma or without comma.What I wanted was that I wanted to split the string such that I get an array with ID:its corresponding string , just like shown below.
["xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:some string without comma","xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:some string,with comma","xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:some string","xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:some string without comma",
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:some string , with comma","xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:String,with comma"]
HOW I ACCOMPLISHED THIS
I used the javascript split function where i split the string by comma followed by 36 characters (for ID) and colon.
String.split(/,(?=.{36}:)/);
PS: I apologize as previously I was not able to ask the question in the correct manner.Hope this time people understand it.
You could use String#split by comma and a look ahead for numbers and colon.
var x = "123456:a,b,c,435213:r,567876:e,363464:t,y,u";
array = x.split(/,(?=\d+:)/);
console.log(array);
For alphanumeric values
var x = "1A3456:a,b,c,43Y213:r,567W76:e,363x64:t,y,u";
array = x.split(/,(?=[a-z0-9]+:)/i);
console.log(array);
You can use the method .split(). I used the "$" as a split sign instead of "," because i thought you would like to keep them.
var values = "123456:a,b,c$435213:r$567876:e$363464:t,y,u".split("$");
var x = values[0];
console.log(values)

Get regex rules for a capture group [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am looking for a method to get the regex rules, for a capture group.
So if I had: /(\w) (\d)/ I would want $1 = /\w/ and $2 = /\d/.
Is there a method for this?
Provided that the regex is valid and no nested groups are used, then you can try this out.
var arr = (regex + "").match(/\(.*?\)/g).map(function(rule){
return rule.replace(/[()]/g, "");
});
Now, arr[0] will have rule of group1, arr[1] will be group2 and so on.
Although I don't know of any direct way to do this on a regex rule given by /(rule1)(rule2)/ you could build the regex rule using strings and specify your grouping in different strings.
var group1 = '([A-Za-z]+)',
group2 = '(\\d+)',
r = new RegExp(group1+group2);
r.test('hello1234');
To get the groups from a regex string you could run a regex on that regex string to extract the groups. If your regex string is "(\w+)(\d+)" then you'd have a regex to extract the groups as /(\([^\)\))+/

Categories

Resources