i have this string
[111] Test Team +3½-110
and i my expected out is this
team : Test Team, bet : +3½-110
how can i do that in jquery?
TIA
You wouldn't use jQuery for this (nor is the "json" tag appropriate), you'd use standard JS string manipulation functions (with or without regex).
I'll assume the rule for splitting up your string is to ignore any leading value in square brackets, then take everything up to the + as the team name and everything after the + (including the + itself) as the bet. If there's more than one + then all but the last will be considered part of the team name.
function formatBet(input) {
return input.replace(/^(\[[^\]]*\] )?(.+)( \+[^+]+)$/,"team: $2, bet:$3");
}
console.log(formatBet("[111] Test Team +3½-110"));
// logs "team: Test Team, bet: +3½-110"
console.log(formatBet("Test Team +3½-110"));
// logs "team: Test Team, bet: +3½-110"
console.log(formatBet("[111] Test + Team +3½-110"));
// logs "team: Test + Team, bet: +3½-110"
console.log(formatBet("[111] Whatever +3½-110"));
// logs "team: Whatever, bet: +3½-110"
Demo: http://jsfiddle.net/LKJjw/
In my regex:
^(\[[^\]]*\] )?
...optionally matches at the beginning of the string a [ followed by zero or more non-] characters followed by ] and a space. Then:
(.+)
...matches the team name - one or more of any character. Then:
( \+[^+]+)$
...matches a space followed by a +, followed by one or more non-+ characters at the end of the string.
] then you can do something like the following: .replace(/^[^\]]*\] (.+)( \+[^+]+)$/,"team: $1, bet:$2");