Regex pattern for following example test test123 [closed] - javascript

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);

Related

How to replace last substring of a string in javascript? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to replace the last substring of a string without splitting the whole string. Exp: "I am a newbie" to "I am a student". But this piece of code not working. Please, help!
var old = "newbie"
var new_ = "student"
var str = str.replace(new RegExp(old + '$'), new_);
Your code is working. But you had not declared the str variable with the value "I am a newbie". You directly tried to replace, which may have caused it to not work.
var old = "newbie"
var new_ = "student"
var str = "I am a newbie"
str = str.replace(new RegExp(old + '$'), new_);
console.log(str)
A more general answer, if you want to replace the last word of the input string by another value, but you do not know what that word will be beforehand, you can use a regular expression.
string.replace(/\w+$/, 'student');
const strings = [
'I am a newbie',
'I like sunshine',
'I do not like pheasants'
];
strings.forEach(e => {
console.log(`Updated ${e} to ${e.replace(/\w+$/, 'student')}`);
});

Javascript - Regex Does not contain some character [closed]

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);

How to escape special charater in javascript's Variable [closed]

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 4 years ago.
Improve this question
I might asking a naive question.
But I am stuck. My requirement is do masking of data.
Following is the code snippet :
var str = substr(Test_dat,0,6);
var Test_dat1 = replace(Test_dat,str,"SampleSample");
So basically, "Test_dat" is input string and I am applying substr() function the on incoming data. And then replacing based on masking logic.
If
var Test_dat = "Vikas(vikas)";
var str = substr(Test_dat,0,5);
var Test_dat1 = replace(Test_dat,str,"SampleSample");
Output
SampleSample(vikas)
If
Input
var Test_dat = "Vikas(vikas)";
var str = substr(Test_dat,0,6);
var Test_dat1 = replace(Test_dat,str,"SampleSample");
Error Message
Function call replace is not valid : Unclosed group near index 6
I know it's because of '(' but I am not able to understand how to escape in variable "str".
Any Help!!
Change the way you use substr and replace. This code works well.
var Test_dat = "Vikas(vikas)";
var str = Test_dat.substr(0,6);
var Test_dat1 = Test_dat.replace(str,"SampleSample");

String replace() fails [closed]

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 7 years ago.
Improve this question
I used the replace() function to remove the _pc and keep the 1, but it's not working...
function testing()
{
var code = "a1_pc"; //The initial stuff
alert(code); //Printing -> a1_pc
var number = code.split("a"); //Remove the "a"
alert(number); //Printing again -> ,1_pc
number = number.slice(1); //Remove the ","
alert(number); //Printing again -> 1_pc
number = number.replace("_pc", "");
alert(number); //Returns nothing...
}
Your above solution should work perfectly and does so in the example below.
The problem must lay somewhere else within your code.
var text = '1_pc';
text = text.replace("_pc", "");
console.log(text);
if you are certain it is the replace() function causing the problems, you can use either of these 2 alternatives.
If you know that the last 3 characters are always _pc, you could use substring to find all the other characters instead.
var text = '1_pc';
text = text.substring(0, text.length - 3);
console.log(text);
Or very similiar to the solution above, you could use slice which is essentially a much cleaner version of the substring solution.
var text = '1_pc';
text = text.slice(0, -3);
console.log(text);
You can use split() javascript function and get first occurrence of string.
split("string which you want to",limit as 1 for first occurrence only)
var res = text.split("_",1);
it will return 1

Regex in JavaScript replace on } with preceding character [closed]

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>

Categories

Resources