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 problem creating a reg expression.
E.g.: i have the following text "This} is} a} test}"
Now i want to replace every } including any preceded character.
Is this possible in java script with Regex?
The result of this text should be.
"Thi i tes"
How about:
str.replace(/.}/g, '')
Try This,
var st = "This} is} a} test}";
var rep = st.replace(/.\}/g, "");
console.log(rep);
<script type="text/javascript">
var test = "This} is} a} test}";
var regexp = new RegExp(".}","g");
test = test.replace(regexp, "");
alert(test); //"Thi i tes"
</script>
Related
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 2 months ago.
Improve this question
I need a regex that detects words inside a string. My regex works fine when the class consists of a single word, but not for multiple words.
function repl(string){
return string.replace(/<span class="[a-z\s\-\b \b]*">/ , (n)=>{
return <Symbol/>
}
}
Works:
repl('<span class='hi'>') /==> <Symbol/>
Does not work:
repl('<span class='hi hi'>')
You can simplify your expression to: /<span class=["'][a-z_\-][\w\s\-]*["']>/i
Make sure your HTML element attributes are double-quoted.
Note: Please be aware that the closing </span> tag is mandatory and should never be ommited.
const repl = (str) =>
str.replace(/<span class=["'][a-z_\-][\w\-\s]*["']>/i, () => '<Symbol />');
console.log(repl('<span class="hi">'));
console.log(repl("<span class='hi hi'>"));
console.log(repl('<span class="one two three ">'));
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 3 years ago.
Improve this question
Regex pattern for following example test test123 .first string between A-za-z and second string should be A-Za-z0-9
examples:
hello world123 (true)
123 hello123 (false)
You can use like ^[a-zA-Z]+[ ][a-zA-Z0-9]+$
function CheckValidation() {
var str = document.getElementById("txtInput").value;
var pattern = new RegExp("^[a-zA-Z]+[ ][a-zA-Z0-9]+$");
var res = pattern.test(str);
document.getElementById("para").innerHTML = res;
}
<input type="text" id="txtInput" />
<button onclick="CheckValidation()">Check</button>
<p id="para"></p>
Used the below and working fine
var pattern = new RegExp("^[a-zA-Z]+[ a-zA-Z0-9]+$");
var res = pattern.test(str);
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 4 years ago.
Improve this question
I try to regex for not contain some character.
I need to show /%7(.*?);/g which dose not contain "=".
I try to input
?!xx=1
and change to
?!( (.?)=(.?) )
But it dose not work.
Please help. Thanks.
//Here is my simple regex
reg = /%7((?!xx=1).*?);/g ;
//Here is my string
str = "%7aa; %7bb=11; %7cc=123; %7xx=1; %7yy; %7zz=2;"
//I need
%7aa; and %7yy;
Instead of using a negative lookahead, try using a ^ block:
const reg = /%7([^=;]+);/g;
The ([^=;]+) bit matches any non-=, the condition you're looking for, and non-;, the character at the end of your regex.
I left the capture group in since your question's regex also contains it.
const reg = /%7([^=;]+);/g;
const str = "%7aa; %7bb=11; %7cc=123; %7xx=1; %7yy; %7zz=2;"
const matches = str.match(reg);
console.log(matches);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
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
Improve this question
Why do I need to use $ sign in var reg and what is the meaning of this line. I am new to programming so please help.
<head>
<script type="text/javascript">
function myPopup2(elem, mg) {
var reg = /^[0-9]+$/;
//alert(reg);
//exit();
if (elem.value.length == 0)
{
alert(mg);
elem.focus();
return false;
}
if (elem.value.match(reg))
{
return true;
}
else
{
alert("this is not a number");
elem.focus();
return false;
}
}
</script>
</head>
<body>
<form>
<input type="text" id="name">
<input type="button" onClick="myPopup2(document.getElementById('name'), 'pleas enter a value')" value="POP2!">
</form>
</body>
$ says the End of Line
just have a look here regexper
/^[0-9]+$/ :
^ - matches only from the beginning of string
$ - matches exactly to the end of string
[] - set of characters
0-9 - 0123456789
+ - one or more occurrence of that which it follows
I highly recommend this site for anything to do with regular expressions: http://www.regular-expressions.info/
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 3 years ago.
Improve this question
How would I go about removing numbers and a space from the start of a string?
For example, from '13 Adam Court, Cannock' remove '13 '.
Search for
/^[\s\d]+/
Replace with the empty string. Eg:
str = str.replace(/^[\s\d]+/, '');
This will remove digits and spaces in any order from the beginning of the string. For something that removes only a number followed by spaces, see BoltClock's answer.
str.replace(/^\d+\s+/, '');
var text = '13 Adam Court, Cannock';
var match = /\d+\s/.exec(text)[0];
text.replace(match,"");
The above solution doesn't work for me. Instead I used this regex below.
var sRegExResult = "Regex Sample99";
sRegExResult.replace(/\s|[0-9]/g, '');