This question already has answers here:
Javascript - return string between square brackets
(6 answers)
Closed 4 years ago.
i want to find all occurrences of [anything-here] in sentence
I [am] John. [yesterday morning] я [русский] and Georgian [ქართული] and Indian [utf8-chars] and with symbols or space [good morning] or [hello, how are you?]
the result must be
[am]
[yesterday morning]
[русский]
[ქართული]
etc.
Thanks
var match = myString.match(/\[.+?\]/g);
Technically, this code will match all sentences between [ and ]
Related
This question already has answers here:
Regex string ends with not working in Javascript
(11 answers)
Closed 1 year ago.
what is way to find if string has new line character(/n) followed by any other character in javascript?
Currently I'm doing this:
if (((mystring).slice(-2)) == "/\n/A") {
//mycode
}
I want to check whether last two characters of string are \n and A.
My thing is not working. Is there any other way to do this?
I want to check whether last two characters of string are \n and A.
You can use String#endsWith
const A = `this is a valid string\nA`;
const B = `this is an invalid string`;
console.log('A', A.endsWith('\nA'));
console.log('B', B.endsWith('\nA'));
This question already has answers here:
How do I split a string with multiple separators in JavaScript?
(25 answers)
Closed 3 years ago.
How to split strings of format like "12-12-1990" or "12/12/1990" where - and / are the delimiters.
I have seen answers using regex but I am not proficient in it. Answers regarding this will be helpful as I am a beginner
try this
"2020-01-31".split(/[/-]/ig)
var dateParts1 = "2020-01-31".split(/[/-]/ig);
console.log(dateParts1);
var dateParts2 = "2020/02/21".split(/[/-]/ig);
console.log(dateParts2);
Regex explain
You can pass a regex into Javascript's split operator. For example:
"12-12-1990".split(/-|\//)
["12", "12", "1990"]
"12/12/1990".split(/-|\//)
["12", "12", "1990"]
You can use split with the delimiter on which you want to split the string
var x="12-12-1990";
console.log(x.split('-'))
This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
Trying to replace all a's in the string ignoring the case.
str = "All a's will be replaced";
str.replace(/a/gi, 'Aa');
As expected, the above line of code produces Aall Aa's will be replAaced
But,
regExp = new RegExp('/a/gi');
str.replace(regExp, 'Aa');
the above line with the use of RegExp pattern produces original string All a's will be replaced
How to achieve expected result?
This question already has answers here:
Regular Expression to find a string included between two characters while EXCLUDING the delimiters
(13 answers)
Closed 5 years ago.
var faxNum = $("#txt_faxnum").val();
//Value : ""kekurt kekt809" <(732) 588-8300>"
I want to split between "< >". Example: Just I need to this section : (732) 588-8300
How to split this string ?
You can try this
var String=faxNum.substring(str.lastIndexOf("<")+1,faxNum.lastIndexOf(">"));
This question already has answers here:
Remove characters from a string [duplicate]
(6 answers)
Closed 7 years ago.
I have the below variable in JavaScript. I want to remove the "comma" sign using jQuery /JavaScript.
var test = ",,2,4,,,3,,3,,,"
Expected output: var test = "2,4,3,3"
please advice
Use replace() with regex
var test = ",,2,4,,,3,,3,,,";
document.write(test.replace(/,+/g,',').replace(/^,|,$/g,''));
replace(/,+/g,',') - To remove multiple comma with one.
replace(/^,|,$/g,'') - To remove comma at starting and ending.