RegEx - Parse through string for specific words - javascript

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]); }

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}`)

How do I split a letter and number?

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);

Find every instance of #<username> in paragraph in Javascript

I have a paragraph that could have 1 or more instances of "#" followed by different usernames. How can I find each instance of this in Javascript?
Something like this but it doesn't work yet:
var regexp = '/#([a-z0-9_]+)/i';
var post = "Hi #tom, where is #john and #nick?" ;
var match, matches = [];
while ((match = regexp.exec(post)) != null) {
matches.push(match.index);
}
console.log(matches);
Console log would read: #tom #john #nick
You have two mistakes in your code causing it not to work as you expect.
(1.) You need to remove the quotes from your regular expression and use the g (global) modifier. You can replace your character class to the shorter version \w and remove the case-insensitive modifier here.
var regexp = /#\w+/g
(2.) You need to reference the match instead of referencing the match.index
matches.push(match[0]);
Final solution:
var post = "Hi #tom, where is #john and #nick?";
var regexp = /#\w+/g
var match, matches = [];
while ((match = regexp.exec(post)) != null) {
matches.push(match[0]);
}
console.log(matches); //=> [ '#tom', '#john', '#nick' ]
Alternatively you can use the String.match method.
var post = 'Hi #tom, where is #john and #nick?',
result = post.match(/#\w+/g);
console.log(result); //=> [ '#tom', '#john', '#nick' ]

Display characters other than alphabets using reqular expression

I have tried to display characters other than alphabets in the particular string but it is displaying only the first char.
var myArray = /[^a-zA-Z]+/g.exec("cdAbb#2547dbsbz78678");
The reason it is only displaying the first character is because with using exec and the g modifier (global), this method is meant to be used in a loop for getting all sub matches.
var str = "cdAbb#2547dbsbz78678";
var re = /[^a-zA-Z]+/g;
var myArray;
while (myArray = re.exec(str)) {
console.log(myArray[0]);
}
Output
#2547
78678
If you were wanting to combine the matches you could use the following.
var str = "cdAbb#2547dbsbz78678",
res = str.match(/[\W\d]+/g).join('');
# => "#254778678"
Or do a replacement
str = str.replace(/[a-z]+/gi, '');
You can do:
"cdAbb#2547dbsbz78678".match(/[^a-zA-Z]+/g).join('');
//=> #254778678
RegExp.exec with g (global) modifier needs to run in loop to give you all the matches.

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