Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
I have a string like:
def definition():
I want to change word def (for example), every instance of word def but not the "def"s that are part of other words
like this
console.log("def definition():".specialReplace("def", "abc"));
and result should be
abc definition():
not
abc abcinition():
Use String#replace or String#replaceAll with a regular expression:
const specialReplace = (str) => str.replaceAll(/\bdef\b/g, 'abc')
console.log(specialReplace("def definition")) // abc definition
console.log(specialReplace("def definition def")) // abc definition abc
In the regular expression, \b is a boundary type assertion that matches any word boundary, such as between a letter and a space.
Note that the same sequence \b is also used inside character class regular expression positions ([\b]), to match the backspace character.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I have a long document with some headings I want to replace in a operation.
The headings have the following structure (there are two nouns, both in with a uppercase first character, separated with a whitespace, also the time is dynamic):
let string = 'Firstname Lastname [00:01:02]';
I want to insert some characters at the front and the end of this string, but want to keep the content.
So the desired output should be something like:
let string = '{Firstname Lastname [00:01:02]}:';
I tried a little bit around with RegEx and can catch the time with the following pattern:
\[[0-9]{2}:[0-9]{2}:[0-9]{2}
I figured it out by using captures in my RegEx.
/(\b[A-Z][a-z]* [A-Z][a-z]*( [A-Z])?\b\s\[[0-9]{2}:[0-9]{2}:[0-9]{2}\])/g
This RegEx captures the pattern of my headings into group one. In a replace operation I can then insert the desired content.
string.replace(regex, '{$1}:')
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to check whether a given file name is in the correct format or not.
Means:
first four numbers_two numbers-two numbers-4 numbers.zip
for that I need a regular expression.
Example file name is (1201_17-11-2015.zip) in javascript
var re = new RegExp('^\d{4}_\d{2}-\d{2}-\d{4}.zip$');
if (filename.match(re)) {
//successful match
}
You regexp could look something like this:
^\d{4}_\d\d-\d\d-\d{4}.zip$
^ is the beginning of your pattern
\d means any number
{n} means that the last pattern has to exist n-time
$ is the end of your pattern
On this site you can start learning how to use regular expression
Here you can test if your own regular expression is working...
I find it best to test scenarios at RegExr. None the less, what you're asking for is basic:
var result = "1201_17-11-2015.zip".match(/\d{4}_\d{2}-\d{2}-\d{4}\.zip/)
if (result == null) {
console.warn("Unable to find a match");
} else {
console.log("Found match: %k", result);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to know how I set a regex pattern for aphanumeric and dollar sign.
Except for dollar sign, it does not accept any other special characters.
Here are examples...
The pattern should be okay with ....
hahah
hohho
hihihi
$hahah
hahah I will get $100 for this
The pattern should be sad with ....
hi James.
#fdasfdas
run!
Any idea?
so you want it to require a '$' symbol somewhere in the string? – yes.
Do you want to allow spaces also? - yes
please add more details, unless the below answer is what you are looking for. Currently this isn't a clear question. – sorry I just got back to my machine.
public static bool IsAlphanumericCharactersAndDollarSign(string str)
{
if (str == null) return false;
Regex rg = new Regex(#"/[a-zA-z0-9\s\$]*/");
return rg.IsMatch(str);
}
Pattern for this: /[a-zA-z0-9\s\$]*/ match alphanumeric, spaces and $ sign 0 or more times
This is PCRE compliant, but in perl for example you need to escape the $ with \$
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to write JS regex (or function) that tells me if a string is in that format:
/root/:param1/:param2/:param3/.../
OR
/root/:param1/:param2/:param3/... (without last slash)
Any ideas?
Thanks.
If I'm interpreting your question correctly, it looks like we can break this pattern down into three primary components:
Start with /root
Followed by some number of /:param
Optionally followed by a /
Now we just need to develop the regular expressions for each component and combine them:
Start with /root
Start of the string is marked by ^ and we follow with /root
^/root
Followed by some number of /:param:
Let's say :param should match 1-N characters (+ operator) that are not a forward slash [^/]
This gives us /[^/]+
0-N of this entire unit can be matched using groups and the * operator: (/[^/]+)*
Optionally followed by a /
Use the ? operator: /?
Append a $ to specify the string's end
All together we get the regular expression ^/root(/[^/]+)*/?$. You can use RegExp.prototype.test to check for matches:
r = new RegExp('^/root(/[^/]+)*/?$')
r.test('/root') // => true
r.test('/root/') // => true
r.test('/root/apple/banana') // => true
r.test('/root/zebra/monkey/golf-cart/') // => true
If you're looking to match a URL path segment you'll need to use a more specific character set instead of the [^/] I used here for :param characters.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a regexp to check the String "incould \w,-,. and not start with ."
I hope the result is
abc ->false
ab_c ->false
a-b_c.->false
a#-b_C. ->true
.a-b_c. ->true
I tried /[^\w-\.]/ only ".a-b_c." was fail, I get false (I hope be true)
and I tried /^\./ can be ".a-b_c." true, but other was fail.
has any body can help me?
If you need to match string including only \w, - or . and not starting with ., then try this:
/^(?!\.)[\w.-]+$/
Details:
^ - search from start of string
(?!\.) - don't match if there is . symbol at this position
[\w.-]+ - match to more than 1 symbols from \w.- set
$ - match to end of string
Testing:
abc -> matched
ab_c -> matched
a-b_c. -> matched
a#-b_C. -> not matched
.a-b_c. -> not matched