Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've a strings, which can have a text like:
'some text user#t12# some text'
'username#John# some text'
'some text usersurname#Malks#'
'userphoto#1.jpg#'
How do I get a text between # and # symbols?
There's a typical structure of the part of the string to search for - type#variable#
type is a JS variable type, it's placed before the first #.
variable is a text that I need to get.
I'm searching for a regexp, that return variable, that is between #...#.
The problem is, I'm not too familiar with regexp, can you help me please?
You need to use capture groups, basically in a regex anything in brackets will be part of the cpature group, in this case you want to capture all the characters between two hashes. The any amount of characters regex is .* so this is what you want to capture between two hashes. Once you execute it you will find the match as second in the array (the first will be the string with the hashes.
var type = "";
var myString = "some text user#t12# some text";
var myRegexp = new RegExp(type+"#(.*)#","g");
var match = myRegexp.exec(myString);
alert(match[1]); // t12
any other matches between hashes will be in match[2].. match[n]
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I am searching for a regular expression that matches and replaces two words within a row with the same but generic prefix and different but definitive suffixes. As a simple example, /x-resses and x-ors/g should match "actresses and actors" and "ancestresses and ancestors". What do I have to replace x with?
The first x should be a capture group containing a pattern that matches the accepted character sequence, the second should be a back reference to it. For example:
const regex = /([a-z]+)resses and \1ors/;
console.log(regex.test('actresses and actors'));
console.log(regex.test('ancestresses and ancestors'));
console.log(regex.test('ancestresses and actors'));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I need to find a regex to extract first occurrence of a string from a data.
For example my data is like :
SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD
I need to extract ABCD123456789 from this data.
I need to find first occurrence of string always starts with ABCD and has total length of 13.
How can I achieve this with using regex?
I tried regex /^ABCD(\w{9})$/gm which didn't work for me.
You can use /ABCD\w{9}/g with match() to get the result from first index:
var str = "SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD"
console.log(str.match(/ABCD\w{9}/g)[0])
The pattern that you tried ^ABCD(\w{9})$ does not match because you use anchors ^ and $ to assert the start and the end of the string.
Note that if you want a full match only, you don't need a capturing group (\w{9})
You can omit those anchors, and if you want a single match from the string you can also omit the /g global flag and the /m multiline flag.
ABCD\w{9}
Regex demo
const regex = /ABCD\w{9}/;
const str = `SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD`;
console.log(str.match(regex)[0])
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I find and extract capitalized words of a string with regex?
I would like to:
extract the capitalized words of a string, as an array
extract the last capitalized word of a string, as a substring:
Both with one regex
If I have this:
var str="This is a STRING of WORDS to search";
I would like to get this 1:
allCapWords // = ["STRING", "WORDS"]
and 2:
lastCapWord // = "WORDS"
To extract the words into an array:
var allCapWords = str.match(/\b[A-Z]+\b/g);
-> ["STRING", "WORDS"]
(Here's a Regex101 test with your string.)
To pull the last word:
var lastCapWord = allCapWords[allCapWords.length - 1];
-> "WORDS"
var str="This is a STRING of WORDS to search";
var regObj = /\b([A-Z]+)\b/g;
allCapWords = str.match(regObj);
You can try this regexpr /\b[A-Z]+\b/gor \b[A-Z0-9]+\b/g if you are interested in catch numbers inside the string
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Can anyone tell me how to replace comma followed by double quotes(",) with double quotes(") in java script
Actually I am getting the string as ",4,34,26,23"
but I want to remove the first comma in the string
also the same when it occurs at the last(,") as below
"4,34,23,54,"
Thanks in Advance
Rakesh
You can use regular expressions like this
var data = ",4,34,26,23,";
data = data.replace(/^,|,$/g, "");
console.log(data);
Output
4,34,26,23
If the double quotes are also part of the original string,
var data = "\",4,34,26,23,\"";
data = data.replace(/^",|,"$/g, "");
If you want to strip only the , and retain ", you can just put the double quotes as the second parameter to the replace, as suggested by #nnnnnn, like this
data = data.replace(/^,|,$/g, "\"");
data = data.replace(/^",|,"$/g, "\"");
var a = ",4,34,26,23";
var replaced=a.replace(',','');
alert(replaced);
Try this
var x = ',4,34,26,23';
x.replace(/^,|,$/g,'');
This removes any starting or ending commas :
",4,34,26,23,".replace(/^,|,$/g,"") // "4,34,26,23"
try this
var str = '",4,34,26,23"';
str = str.replace('",','"');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string like
var str=" , this, is a ,,, test string , , to find regex,,in js. , ";
in which there are multiple spaces in beginning,middle and end of string with commas. i need this string in
var str="this is a test string to find regex in js.";
i found many regex in forum removing spaces , commas separately but i could not join them to remove both.
Please give explanation of regex syntex to if possible .
Thanks in advance
You can just replace every space and comma with space then trim those trailing spaces:
var str=" , this, is a ,,, test string , , to find regex,,in js. , ";
res = str.replace(/[, ]+/g, " ").trim();
jsfiddle demo
you can use reg ex for this
/[,\s]+|[,\s]+/g
var str= "your string here";
//this will be new string after replace
str = str.replace(/[,\s]+|[,\s]+/g, 'your string here');
RegEx Explained and Demo
Try something like this:
var new_string = old_string.replace(/[, ]+/g,' ').trim();
The regex is simply [, ]+ if we were to break this down the \s means ANY whitespace character and , is a literal comma. The [] is a character set (think array) and the + means one or more matches.
We throw in a /g on the end so that it does a global search and replace, otherwise it'd just do it for one match only.
You should be able to use
str.replace(/,/g," ");
The 'g' is the key, you may need to use [,]