0

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

0

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks it works like a charm! cheers! how about if it has description like this description text here[111] Test Team +3½-110
If you know that you only want the text after the first ] then you can do something like the following: .replace(/^[^\]]*\] (.+)( \+[^+]+)$/,"team: $1, bet:$2");
0
var str = '[111] Test Team +3½-110';
str.replace('[111] Test Team','team : Test Team, bet :');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.