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 3 years ago.
Improve this question
I want to replace the "3" into "5". When it is using static it's working fine but when I use it through variable var allvar= '"3"'; it's not working fine.
Here is the jsfiddle link
new RegExp( /[allvar]+/g ); will construct a regular expression matching all uninterrupted sequences of one or more characters from the set a, l, v, a, r.
To construct a regular expression from a variable, you can do this:
new RegExp(allvar, 'g')
It would also be good to escape characters with special meaning to RegExp, unless you intend for allvar to contain regexp source. Unfortunately, RegExp.escape is still not in the language, so one would use a workaround.
new RegExp(escapeRegExp(allvar), 'g')
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 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.
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
How to split string with several char
i have to split a scope when there are spaces, comma, dash, etc..( ponctuation) and when words are concatenated ( without a space between variable)
For exemple
testOne="{{test.test}} {{test.test}}{{test.test}}";
The expected output is
"test.test test.testtest.test"
(There are two comma between the first and the second text.text)
You can use Regular Expression to get the result what you wanted.
testOne = "{{test.test}} {{test.test}}{{test.test}}";
console.log(testOne.match(/{{.*?}}/g).map(function(item) {
return item.replace(/[{}]/g, "");
}));
# [ 'test.test', 'test.test', 'test.test' ]
You can achieve your desired output with a simple replace
testOne.replace(/{{(.*?)}}/g, '$1');
// "test.test test.testtest.test"
Remember to set this back to testOne if you want it kept as that variable
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
How to get this string to give me the exact matching result
session[username_or_email]
the expression
<html>
<body>
<script type="text/javascript">
var str="session[username_or_email]";
var patt1=/ID|un|name|login_username|userid|username|user|Email|uname|usr|log|email|mail|nick|CUST|account|wpName1|textbox|pw|session[username_or_email]/i;
document.write(str.match(patt1));
</script>
</body>
</html>
the result now is username
Thankyou.
Just escape the [ with \[ so it isn't treated as a special regex control character, but just a normal character to search for.
var str="session[username_or_email]";
var patt1=/ID|un|name|login_username|userid|username|user|Email|uname|usr|log|email|mail|nick|CUST|account|wpName1|textbox|pw|session\[username_or_email]/i;
document.write(str.match(patt1));
You can see it work here: http://jsfiddle.net/jfriend00/CuWKV/
You don't actually have to escape the ] because it is only an expected regex control character when a [ has come before it though it does not harm to escape it also.
Please note the escaped \[ - I also moved the wanted string to the beginning since it contains username too - this may or may not be what you need, but you should consider it.
var str="session[username_or_email]";
var patt1=/session\[username_or_email\]|ID|un|name|login_username|userid|username|user|Email|uname|usr|log|email|mail|nick|CUST|account|wpName1|textbox|pw/i;
document.write(str.match(patt1));