How do I split a letter and number? - javascript

I have this string
let tmp = "abcd1234";
I tried below code but didnt worked. can anyone pls. advice.
let tmp = "abcd1234";
var alphas = tmp.split("(?<=\\D)(?=\\d");
console.log(alphas[0],'---',alphas[1])
Its returning "abcd1234 --- undefined"
Thanks in Advance.

If you're sure you will have alpha, then numeric, then look for the point where it changes, append a space, then split on it :
const tmp = "abcd1234";
const [alpha, numeric] = tmp.replace(/(\D)(\d)/, '$1 $2').split(' ');
console.log(alpha, '---', numeric);

let tmp = "abcd1234";
var alphas = tmp.split(/(\d+)/);
console.log(alphas[0], '---', alphas[1])
simple regexp /(\d+)/ that will find numbers in row and split from letters

Your regex is (?<=\\D)(?=\\d, it appears that you're missing a closing bracket ) at the end of your regex. The complete regex then becomes (?<=\\D)(?=\\d).
Also you're enclosing your regex in "regex" and it should be enclosed in /regex/
let tmp = "abcd1234";
var alphas = tmp.split(/(?<=\D)(?=\d)/);
console.log(alphas);
console.log(alphas[0],'---',alphas[1])
Based on a comment by #trichetriche who said positive lookbehind is not supported on all browsers, a simpler method would be to enclose the letters and numbers within their own capturing group like this:
const regex = /(\D+)(\d+)/;
const str = "abcd1234";
let alphas = regex.exec(str);
console.log(alphas[1], '---', alphas[2])

You can do it with regex too:
let tmp = "abcd1234";
let myRegexp = /([a-z]+)([1-9]+)/;
var match = myRegexp.exec(tmp);
console.log(match[1],'---',match[2])

Could you use Regex? this could be a starter solution
let tmp = "abcd1234";
var n = /\d+/.exec(tmp);
var c = /[a-zA-Z]+/.exec(tmp);
console.log(n[0],'---',c[0])
From here you should to control if there are multiples matches and so on.
Note: \D+ will match every caracter non digit so =+. etc will match.
More Regex info: here
Regex playground : here

const tmp = "abcdABCD1234";
const [alpha, numeric] = tmp.split(/(\d+)/);
console.log(alpha, '---', numeric);

Related

Javascript get only matched text in regex

I have string like below
BANKNIFTY-13-FEB-2020-31200-ce
I want to convert the string to 13-FEB-31200-ce
so I tried below code
str.match(/(.*)-(?:.*)-(?:.*)-(.*)-(?:.*)-(?:.*)/g)
But its returning whole string
Two capture groups is probably the way to go. Now you have two options to use it. One is match which requires you to put the two pieces together
var str = 'BANKNIFTY-13-FEB-2020-31200-ce'
var match = str.match(/[^-]+-(\d{2}-[A-Z]{3}-)\d{4}-(.*)/)
// just reference the two groups
console.log(`${match[1]}${match[2]}`)
// or you can remove the match and join the remaining
match.shift()
console.log(match.join(''))
Or just string replace which you do the concatenation of the two capture groups in one line.
var str = 'BANKNIFTY-13-FEB-2020-31200-ce'
var match = str.replace(/[^-]+-(\d{2}-[A-Z]{3}-)\d{4}-(.*)/, '$1$2')
console.log(match)
Regex doesn't seem to be the most appropriate tool here. Why not use simple .split?
let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let splits = str.split('-');
let out = [splits[1], splits[2], splits[4], splits[5]].join('-');
console.log(out);
If you really want to use regexp,
let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let splits = str.match(/[^-]+/g);
let out = [splits[1], splits[2], splits[4], splits[5]].join('-');
console.log(out);
I would not use Regex at all if you know exact positions. Using regex is expensive and should be done differently if there is way. (https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/)
const strArr = "BANKNIFTY-13-FEB-2020-31200-ce".split("-"); // creates array
strArr.splice(0,1); // remove first item
strArr.splice(2,1); // remove 2020
const finalStr = strArr.join("-");
If the pattern doesn't need to be too specific.
Then just keep it simple and only capture what's needed.
Then glue the captured groups together.
let str = 'BANKNIFTY-13-FEB-2020-31200-ce';
let m = str.match(/^\w+-(\d{1,2}-[A-Z]{3})-\d+-(.*)$/)
let result = m ? m[1]+'-'+m[2] : undefined;
console.log(result);
In this regex, ^ is the start of the string and $ the end of the string.
You can have something like this by capturing groups with regex:
const regex = /(\d{2}\-\w{3})(\-\d{4})(\-\d{5}\-\w{2})/
const text = "BANKNIFTY-13-FEB-2020-31200-ce"
const [, a, b, c] = text.match(regex);
console.log(`${a}${c}`)

Split a string and get the second last comma

I have a string "Fred/Jim/Rob/"
What I needed is I need the split the string till last and also avoid the last /.
I have tried with:
for (var i = 0; i < input.length; i++) {
var input = ["Fred/Jim/Rob/"]
var X = input.split("/",);
----some other code---
}
In that case, my loop is running till last /, So I want to just avoid last /.
Consider using .match instead - match non-/ characters with a regular expression:
const str = "Fred/Jim/Rob/";
const result = str.match(/[^/]+/g);
console.log(result);
You might also split on a forward slash / and filter the empty entries afterwards using Boolean.
const input = "Fred/Jim/Rob/";
const result = input.split("/");
console.log(result.filter(Boolean));
You can simply remove the last / and split,
let str = "Fred/Jim/Rob/"
let str2 = "Fred/Jim/Rob"
let newStr =(str)=> (str.endsWith('/') ? str.substr(0,str.length-1) : str).split('/')
console.log(newStr(str))
console.log(newStr(str2))
You can try following :
words = "Fred/Jim/Rob/".split('/');
words.pop();
console.log(words);
It is possible to use split method:
let input = "Fred/Jim/Rob/";
let [fred, jim, Rob] = input.split(/[ / ]/);
console.log([fred, jim, Rob]);
try something like this:
var input = ["Fred/Jim/Rob/"];
var slices = input.split("/");
console.log(slices[slices.length-1]);

RegEx - Parse through string for specific words

I'm looking to parse through a string and find all the handles (#name) and push them each into one array (without the # though) so I can loop through them (with forEach) and send them each an alert. Each handle is separated by a space.
If you just need to extract the users from a tweet, you can use the following regex:
/#([a-zA-Z0-9]+)/g
For example:
var string = '#JohnSmith #DylanThompson Hey guys!';
var numberPattern = /#([a-zA-Z0-9]+)/g;
var res = string.match(numberPattern);
console.log(res);
This would spit out:
["#JohnSmith", "#DylanThompson"]
You can capture #followedByName and than replace #
let str = `#someName hey #someMoreNames`
let op = str.match(/(^|\s)#\w+/g).map(e=>e.trim().replace(/#/g,''))
console.log(op)
try
let str= "Here #ann and #john go to #jane";
let m= str.match(/#\w+/g).map(x=>x.replace(/./,''));
m.forEach(x=> console.log(x));
You can use also positive lookbehind regexp but it is not supported by firefox yet (but it is part of ES2018):
let str= "Here #ann and #john go to #jane";
let m= str.match(/(?<=#)\w+/g);
m.forEach(x=> console.log(x));
where (?<=#)\w+ match word which start after # (excluding this char - positive lookbehind)
You can combine match to extract names and slice to remove #:
str = "#JohnSmith #DylanThompson Hey guys";
let arr = str.match(/#\w+/g).map(e=>e.slice(1));
console.log(arr);
Try This:
var str= "Here #ann and #john go to #jane";
var patt = /#(\w+)/g;
while ( (arr = patt.exec(str)) !== null ) { console.log(arr[1]); }

RegEx: remove |

var a="value1%7Cvalue2=%20a%20|value3"
url is encoded in such a way that for some values it is encoded as %7C and some places it is | sign only.
Without decoding this string how to remove everything that comes after first | using regular expression?
Using a regex, as you asked:
var a = "value1%7Cvalue2=%20a%20|value3"
var regex = /\|.*/;
a = a.replace(regex, "");
console.log(a);
We match the | followed by an unlimited number of characters, and replace the match with the empty string.
It's much easier to do with a split, though.
a = a.split('|')[0]
console.log(a)
Why regex? Try split.
var a="value1%7Cvalue2=%20a%20|value3";
var b = a.split('|')[0];
see this demo https://regex101.com/r/dE0jW4/2
/([^|]*)\|.*/
var re = /([^|]*)\|.*/gm;
var str = 'value1%7Cvalue2=%20a%20|value3"';
var subst = '$1';
var result = str.replace(re, subst);

Regex remove repeated characters from a string by javascript

I have found a way to remove repeated characters from a string using regular expressions.
function RemoveDuplicates() {
var str = "aaabbbccc";
var filtered = str.replace(/[^\w\s]|(.)\1/gi, "");
alert(filtered);
}
Output: abc
this is working fine.
But if str = "aaabbbccccabbbbcccccc" then output is abcabc.
Is there any way to get only unique characters or remove all duplicates one?
Please let me know if there is any way.
A lookahead like "this, followed by something and this":
var str = "aaabbbccccabbbbcccccc";
console.log(str.replace(/(.)(?=.*\1)/g, "")); // "abc"
Note that this preserves the last occurrence of each character:
var str = "aabbccxccbbaa";
console.log(str.replace(/(.)(?=.*\1)/g, "")); // "xcba"
Without regexes, preserving order:
var str = "aabbccxccbbaa";
console.log(str.split("").filter(function(x, n, s) {
return s.indexOf(x) == n
}).join("")); // "abcx"
This is an old question, but in ES6 we can use Sets. The code looks like this:
var test = 'aaabbbcccaabbbcccaaaaaaaasa';
var result = Array.from(new Set(test)).join('');
console.log(result);

Categories

Resources