Is there an easy way to replace characters at the beginning and end of a string, but not in the middle? I need to trim off dashes. I know trim() exists, but it only trims whitespace.
Here's my use case:
Input:
university-education
-test
football-coach
wine-
Output:
university-education
test
football-coach
wine
You can use String#replace with a regular expression.
^-*|-*$
Explanation:
^ - start of the string
-* matches a dash zero or more times
| - or
-* - matches a dash zero or more times
$ - end of the string
function trimDashes(str){
return str.replace(/^-*|-*$/g, '');
}
console.log(trimDashes('university-education'));
console.log(trimDashes('-test'));
console.log(trimDashes('football-coach'));
console.log(trimDashes('--wine----'));
I would suggest using the trim function of lodash. It does exactly what you want. It has a second parameter which allows you to pass the characters which should be trimmed. In your case you could use it like this:
trim("-test", "-");
The 'trim' function here is inadequate. You can catch this gap using 'RegEx' within the 'replace' function.
let myText = '-education';
myText = myText.replace(/^\-+|\-+$/g, ''); // output: "education"
Use in the array
let myTexts = [
'university-education',
'-test',
'football-coach',
'wine',
];
myTexts = myTexts.map((text/*, index*/) => text.replace(/^\-+|\-+$/g, ''));
/* output:
(4)[
"university-education",
"test",
"football-coach",
"wine"
]
*/
/^\ beginning of the string, dashe, one or more times
| or
\-+$ dashe, one or more times, end of the string
/g 'g' is for global search. Meaning it'll match all occurrences.
Sample:
const removeDashes = (str) => str.replace(/^\-+|\-+$/g, '');
/* STRING EXAMPLE */
const removedDashesStr = removeDashes('-education');
console.log('removedDashesStr', removedDashesStr);
// ^^ output: "removedDashesStr education"
let myTextsArray = [
'university-education',
'-test',
'football-coach',
'wine',
];
/* ARRAY EXAMPLE */
myTextsArray = myTextsArray.map((text/*, index*/) => removeDashes(text));
console.log('myTextsArray', myTextsArray);
/*^ outpuut:
myTextsArray [
"university-education",
"test",
"football-coach",
"wine"
]
*/
Related
I am trying to split a string so that I can separate it depending on a pattern. I'm having trouble getting the correct regex pattern to do so. I also need to insert the results into an array of objects. Perhaps by using a regex pattern, the string can be split into a resulting array object to achieve the objective. Note that the regex pattern must not discriminate between - or --. Or is there any better way to do this?
I tried using string split() method, but to no avail. I am trying to achieve the result below:
const example1 = `--filename test_layer_123.png`;
const example2 = `--code 1 --level critical -info "This is some info"`;
const result1 = [{ name: "--filename", value: "test_layer_123.png" }];
const result2 = [
{ name: "--code", value: "1" },
{ name: "--level", value: "critical" },
{ name: "-info", value: "This is some info" },
];
If you really want to use Regex to solve this.
Try this Pattern /((?:--|-)\w+)\s+"?([^-"]+)"?/g
Code example:
function matchAllCommands(text, pattern){
let new_array = [];
let matches = text.matchAll(pattern);
for (const match of matches){
new_array.push({name: match.groups.name, value: match.groups.value});
}
return new_array;
}
let RegexPattern = /(?<name>(?:--|-)\w+)\s+"?(?<value>[^-"]+)"?/g;
let text = '--code 1 --level critical -info "This is some info"';
console.log(matchAllCommands(text, RegexPattern));
Here is a solution that splits the argument string using a positive lookahead, and creates the array of key & value pairs using a map:
function getArgs(str) {
return str.split(/(?= --?\w+ )/).map(str => {
let m = str.match(/^ ?([^ ]+) (.*)$/);
return {
name: m[1],
value: m[2].replace(/^"(.*)"$/, '$1')
};
});
}
[
'--filename test_layer_123.png', // example1
'--code 1 --level critical -info "This is some info"' // example2
].forEach(str => {
var result = getArgs(str);
console.log(JSON.stringify(result, null, ' '));
});
Positive lookahead regex for split:
(?= -- positive lookahead start
--?\w+ -- expect space, 1 or 2 dashes, 1+ word chars, a space
) -- positive lookahead end
Match regex in map:
^ -- anchor at start of string
? -- optional space
([^ ]+) -- capture group 1: capture everything to next space
-- space
(.*) -- capture group 2: capture everything that's left
$ -- anchor at end of string
I have this string:
|AL;GF=0;ID=17;AF=122|CT;GF=0;ID=15;AF=123|BD;GF=0;ID=1;AF=124|
I want to match if CT block (have CT; between |) has ID=1, i tried:
/\|CT;.*?ID=1;(?=.*\|)/
But doesn't working.
Code:
let string = '|AL;GF=0;ID=17;AF=122|CT;GF=0;ID=15;AF=123|BD;GF=0;ID=1;AF=124|'
console.log(string.match(/\|CT;.*?ID=1;(?=.*\|)/g))
// return ['|CT;GF=0;ID=15;AF=123|BD;GF=0;ID=1;']
// expected null
Someone can help me?
You can use
/\|CT;[^|]*ID=1;/
See the regex demo.
Details:
\|CT; - |CT; string
[^|]* - zero or more chars other than a | char
ID=1; - a fixed string.
See a JavaScript demo:
const strings = [
'|AL;GF=0;ID=17;AF=122|CT;GF=0;ID=15;AF=123|BD;GF=0;ID=1;AF=124|',
'|AL;GF=0;ID=17;AF=122|CT;GF=0;ID=1;AF=123|BD;GF=0;ID=1;AF=124|'
]
for (const string of strings) {
console.log(string.match(/\|CT;[^|]*ID=1;/)?.[0]);
}
I want to have a regular expression in JavaScript which help me to validate a string with contains only lower case character and and this character -.
I use this expression:
var regex = /^[a-z][-\s\.]$/
It doesn't work. Any idea?
Just use
/^[a-z-]+$/
Explanation
^ : Match from beginning string.
[a-z-] : Match all character between a-z and -.
[] : Only characters within brackets are allowed.
a-z : Match all character between a-z. Eg: p,s,t.
- : Match only strip (-) character.
+ : The shorthand of {1,}. It's means match 1 or more.
$: Match until the end of the string.
Example
const regex= /^[a-z-]+$/
console.log(regex.test("abc")) // true
console.log(regex.test("aBcD")) // false
console.log(regex.test("a-c")) // true
Try this:
var regex = /^[-a-z]+$/;
var regex = /^[-a-z]+$/;
var strs = [
"a",
"aB",
"abcd",
"abcde-",
"-",
"-----",
"a-b-c",
"a-D-c",
" "
];
strs.forEach(str=>console.log(str, regex.test(str)));
Try this
/^[a-z-]*$/
it should match the letters a-z or - as many times as possible.
What you regex does is trying to match a-z one single time, followed by any of -, whitespace or dot one single time. Then expect the string to end.
Use this regular expression:
let regex = /^[a-z\-]+$/;
Then:
regex.test("abcd") // true
regex.test("ab-d") // true
regex.test("ab3d") // false
regex.test("") // false
PS: If you want to allow empty string "" to pass, use /^[a-z\-]*$/. Theres an * instead of + at the end. See Regex Cheat Sheet: https://www.rexegg.com/regex-quickstart.html
I hope this helps
var str = 'asdadWW--asd';
console.log(str.match(/[a-z]|\-/g));
This will work:
var regex = /^[a-z|\-|\s]+$/ //For this regex make use of the 'or' | operator
str = 'test- ';
str.match(regex); //["test- ", index: 0, input: "test- ", groups: undefined]
str = 'testT- ' // string now contains an Uppercase Letter so it shouldn't match anymore
str.match(regex) //null
I have a string separated by forward slashes, and wildcards are denoted by beginning with a $:
/a/string/with/$some/$wildcards
I need a regex to get all wildcards (without the "$"), where wildcards can either have more "string" ahead of them (and the next character should always be a forward slash) or will be at the end of the string. Here is where I'm at (it matches to the end of the string rather to the next "/"):
//Just want to match $one
var string = "/a/string/with/$one/wildcard"
var re = /\$(.*)($|[/]?)/g
var m = re.exec(string)
console.log(m);
// [ '$one/wildcard',
// 'one/wildcard',
// '',
// index: 123,
// input: '/a/string/with/$one/wildcard'
// ]
Here was a previous attempt (that doesn't account for wildcards that are at the end of the string):
//Want to match $two and $wildcards
var string = "/a/string/with/$two/$wildcards"
var re = /\$(.*)\//g
var m = re.exec(string)
console.log(m);
// [ '$two/',
// 'two',
// '',
// index: 123,
// input: '/a/string/with/$two/$wildcards'
// ]
I've searched around for matching a character or end of string and have found several answers, but none that try to account for multiple matches. I think I need the ability to match the next character as a / greedily, and then try to match the end of the string.
The desired functionality is to take the input string:
/a/string/with/$two/$wildcards
and transform it to the following:
/a/string/with/[two]/[wildcards]
Thanks in advance! Also, apologies if this has been explicitly covered in detail, I was unable to find a replica after various searches.
I think this should do it:
/\$([^\/]+)/g
And the you can use the replace() function:
"/a/string/with/$two/$wildcards".replace(/\$([^\/]+)/g, "[$1]");
// "/a/string/with/[two]/[wildcards]"
You can use replace function on the string like so:
var s = '/a/string/with/$two/$wildcards';
s.replace(/\$([a-zA-Z]+)/g, '[$1]')';
s will have the value:
/a/string/with/[two]/[wildcards]
Here's a reference to replace documentation https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace
I have this string (notice the multi-line syntax):
var str = ` Number One: Get this
Number Two: And this`;
And I want a regex that returns (with match):
[str, 'Get this', 'And this']
So I tried str.match(/Number (?:One|Two): (.*)/g);, but that's returning:
["Number One: Get this", "Number Two: And this"]
There can be any whitespace/line-breaks before any "Number" word.
Why doesn't it return only what is inside of the capturing group? Am I misundersating something? And how can I achieve the desired result?
Per the MDN documentation for String.match:
If the regular expression includes the g flag, the method returns an Array containing all matched substrings rather than match objects. Captured groups are not returned. If there were no matches, the method returns null.
(emphasis mine).
So, what you want is not possible.
The same page adds:
if you want to obtain capture groups and the global flag is set, you need to use RegExp.exec() instead.
so if you're willing to give on using match, you can write your own function that repeatedly applies the regex, gets the captured substrings, and builds an array.
Or, for your specific case, you could write something like this:
var these = str.split(/(?:^|\n)\s*Number (?:One|Two): /);
these[0] = str;
Replace and store the result in a new string, like this:
var str = ` Number One: Get this
Number Two: And this`;
var output = str.replace(/Number (?:One|Two): (.*)/g, "$1");
console.log(output);
which outputs:
Get this
And this
If you want the match array like you requested, you can try this:
var getMatch = function(string, split, regex) {
var match = string.replace(regex, "$1" + split);
match = match.split(split);
match = match.reverse();
match.push(string);
match = match.reverse();
match.pop();
return match;
}
var str = ` Number One: Get this
Number Two: And this`;
var regex = /Number (?:One|Two): (.*)/g;
var match = getMatch(str, "#!SPLIT!#", regex);
console.log(match);
which displays the array as desired:
[ ' Number One: Get this\n Number Two: And this',
' Get this',
'\n And this' ]
Where split (here #!SPLIT!#) should be a unique string to split the matches. Note that this only works for single groups. For multi groups add a variable indicating the number of groups and add a for loop constructing "$1 $2 $3 $4 ..." + split.
Try
var str = " Number One: Get this\
Number Two: And this";
// `/\w+\s+\w+(?=\s|$)/g` match one or more alphanumeric characters ,
// followed by one or more space characters ,
// followed by one or more alphanumeric characters ,
// if following space or end of input , set `g` flag
// return `res` array `["Get this", "And this"]`
var res = str.match(/\w+\s+\w+(?=\s|$)/g);
document.write(JSON.stringify(res));