0
I get a value this way: /{mensagem}
and if it were different from that (had not the /
before the {mensagem)
, I wanted him to return me error.
{mensagem}
serves as variable and cannot be changed.
0
I get a value this way: /{mensagem}
and if it were different from that (had not the /
before the {mensagem)
, I wanted him to return me error.
{mensagem}
serves as variable and cannot be changed.
4
If valid strings are in the form /{mensagem}
, with the string value {mensagem}
already saved in a variable, No need for Regex! :)
var str = "/msg válida";
var msg_valida = "msg válida";
if(str == "/" + msg_valida) console.log("Mensagem válida!");
However, if your original goal was to make Regex a little more flexible so as to allow certain snippets of it to be dictated by arbitrary strings, you can use the syntax
var regex = new RegExp("prefixo" + variavel + "sufixo");
The example below illustrates, in a didactic way, how to use this form of regular expression declaration.
// Declaração das entradas (strings) de teste:
var strs = [
"Teste 1",
"/Teste 2",
"/",
"Teste/3",
"/msg"
];
// Variável que representa o "miolo" do RegEx. Pode utilizar sintaxe "RegExpiana", ou seja: \d, *, ...
var msg = "msg";
// Declaração do nosso objeto RegEx:
var regex = new RegExp("^\/" + msg + "$");
// Testamos entrada por entrada...
for (var i = 0; i < strs.length; i++) {
// Se corresponderem à expressão regular compilada na variável "regex"...
if(regex.test(strs[i])){
// Exibimos esta entrada (que, agora sabemos, é VÁLIDA) na tela:
document.getElementById("resultado").innerHTML += "<li>" + strs[i] + "</li>";
}
}
<!-- O HTML serve apenas para exibir o resultado deste snippet. -->
<h2>Strings aprovadas:</h2>
<ul id="resultado"></ul>
3
It seems to me that what you want is to know if the string starts with a bar /
, or not.
Having a given string, to know if it starts with a given character have several different ways. Using regex for something so simple doesn’t seem necessary.
var str = 'string de teste';
Using the .charAt()
can extract the character at the desired position.
str.charAt(0)
gives the letter "s".
Using the .substring()
can extract a part of the string, for example between position 0 and 1.
str.substring(0, 1)
gives the letter "s".
You can test using regex. In this case you should use the string start symbol followed by the character you want. In this case being a bar it should be escaped with \
(not to be interpreted as closing regex).
/^\//.test(str);
gives false, ie the string nay starts with a bar /
3
Test it out here;
/^\/\{mensagem\}$/.test('/{mensagem}') //true
or
/^\/\{.*\}$/.test('/{aqui vem a mensagem do seu usuário}') //true
If this message can be anywhere inside a larger string then remove the ^
of the beginning and the $
of the end
If you also want to extract the message:
var match = '/{aqui vem a mensagem do seu usuário}'.match(/^\/\{(.*)\}$/);
if(match) {
console.log(match[1]); //aqui vem a mensagem do seu usuário
} else {
//retorne o seu erro aqui
}
If the {}
are just placeholders that Voce used, take a look at the @Rui Pimentel response
That’s right, @renatoargh :) I formulated my answer as an alternative interpretation of the question; but I think it’s more like that, really!
Browser other questions tagged javascript regex
You are not signed in. Login or sign up in order to post.
The regular expression to be used would be similar to that
/\/\w+/g.test(variavel_mensagem)
– Wallace Maxters
What does the
{}
here? they are characters from a string? want to compare the content after the bar/
or just check if the bar is there?– Sergio
Regex to something like this doesn’t make sense. I would say that more than half of the solutions that require regex here of the site should be solved with simple string operations.
– Bacco
Hi, you deleted the question you had answered. Was there something wrong with the answer? By the way you can review this question and the answers and accept some case has solved the problem.
– Sergio
Sorry.. change in project.
– Vinícius Lara